开发者

How to have condition in ActiveRecord update

开发者 https://www.devze.com 2022-12-20 00:18 出处:网络
I am trying to updatea record. Am using the following code. Proddiscount.update({:prodid => params开发者_运维知识库[:id]}, {:discount=>params[:discount]})

I am trying to update a record. Am using the following code.

Proddiscount.update({:prodid => params开发者_运维知识库[:id]}, {:discount=>params[:discount]})

Query Im trying to have is:

update proddiscount set discount = '' where prodid = ''


If prodif is not a primary key, use update_all (read more here)

Proddiscount.update_all("discount = #{params[:discount]}", ["prodid = ?", params[:prodid])

But it won't trigger validations.


How about doing this:

@prod = Proddiscount.find(:first, :conditions => {prodid => params[:id]})
@prod.update_attributes({:discount=>params[:discount]})
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号