how rails “update” multiple columns correctly - (updated_at should be updated as well)

后端 未结 3 1581
忘了有多久
忘了有多久 2021-01-21 06:06

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

3条回答
  •  天涯浪人
    2021-01-21 06:12

    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)
    

提交回复
热议问题