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]})
精彩评论