ruby-on-railsrubyupdate-attributesbefore-save

Rails: Data not getting saved with assign_attributes in before save


I am doing an update in before_save as below:

has_many :things

before_save :set_things

def set_things
  things = all_things.map do |t|
    t.assign_attributes(attrs) 
    t
  end
  self.things = things
end

When i save the object as t.save! , the objects has updated attributes, but once reloaded its going back to old value, so updation is not persisting. If i use, t.update_attributes it is working fine, but isnt assign_attributes supposed to persist on save? Why isnt it working ?


Solution

  • This got solved. Since, i was updating an existing record, assign_attribute wasnt updating attributes. Only new record will be updated accroding to this: Rails: Why “collection=” doesn't update records with existing id?