Home > database >  before_save assign a hash of attributes activerecord
before_save assign a hash of attributes activerecord

Time:12-29

i want to change some value in before_save callback. normally I see people do this:

def this_is_a_callback
  self.attr1 = 'xxx'
  self.attr2 = 'yyy'
end

I have it in form of { attr1: 'xxx', attr2 = 'yyy' }, how should I inject them to the model?

I tried assign_attributes and self.attributes= but they somehow have stack level too deep problem. Thanks

CodePudding user response:

I tried assign_attributes and self.attributes= but they somehow have stack level too deep problem.

Look for existing callbacks that are being triggered by these assignments. Likely there is a circular reference there. If just reading the code doesn't work

  • Related