开发者

How to have condition in ActiveRecord update

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜