I am having trouble updating multiple columns properly with rails activerecords. I want to use something like update which basically gets updated_at updated, but i just cant pas
When you mention you want to update multiple columns
- I presume this would be for a single record?
This line will update a collection
response (.where
returns a collection
rather than single object
):
retval = Invoice.where(:id => @invoice.id).update_all(:payment_total => 20,
:due_amount => 10,
:data => clonedata.to_json)
If you're looking to update a single record, I would use the update method like this:
@invoice = Invoice.update(params[id], payment_total: "20", due_amount: "10", data: clonedata.to_json)
Updates all, This method constructs a single SQL UPDATE statement and sends it straight to the database. It does not instantiate the involved models and it does not trigger Active Record callbacks or validations. Values passed to update_all will not go through ActiveRecord's type-casting behavior. It should receive only values that can be passed as-is to the SQL database.
As such, it does not trigger callbacks nor validations - and timestamp update is made in a callback.update_at is a call back
#update
is the method you want for this. If it's not working correctly, that means you're not calling it the way you think you are. Maybe try calling #update!
and see if you get an exception raised?