I currently have the following controller method:
def update_all
params[:users].each do |product, user|
@product = Product.find(product)
@product.update_attribute(:user_id, user)
end
redirect_to :back, :flash => { :notice => "Updated users" }
end
This works as expected, however the problem is that the params are being passed via a select_tag, which has a default value — so if just one select tag is changed and the form is submitted, all the other records will be updated to the default value. How can I check if the attribute has change from its previous value, and only if it has changed to update it?
you can check for dirty attributes and presence
or set the default values in your select tag (from Sending extra param value via select_tag)
so you don’t have to worry about default values.