Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoid, after_create hook cannot modify self

Tags:

mongoid

The below after create hook does not successfully set the gdoc key. We have to use self.write_attribute instead. Am I trying to do something stupid?

class GoogleDoc
  field :gdoc_key, type: String
  field :filename, type: String

  after_create :after_create_hook
  def after_create_hook
    self.gdoc_key =  "qwerty"
    self.save
  end
end

Thanks! Jonathan

like image 743
Jonathan Leung Avatar asked Aug 14 '26 21:08

Jonathan Leung


1 Answers

From Durran

You cannot call save in an after_ hook since you're going to cause the document to fire the callbacks in an infinite loop. You'll need to use something that doesn't fire callbacks, like update_attribute.

https://github.com/mongoid/mongoid/issues/2974

like image 190
Jonathan Leung Avatar answered Aug 21 '26 16:08

Jonathan Leung



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!