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
- Look closely for other callbacks in the error backtrace array.
- See the "Debugging callbacks" section in the ActiveRecord::Callbacks documentation. Examine the callbacks returned by
MyModel._save_callbacks
to see if they are what you expect. Check each for recursions, e.g. by commenting them out.