Mongoid,after_create hook无法修改self

时间:2013-04-16 00:15:47

标签: mongoid

创建挂钩后面的下面没有成功设置gdoc键。我们必须使用self.write_attribute代替。我想做些蠢事吗?

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

谢谢! 乔纳森

2 个答案:

答案 0 :(得分:4)

来自Durran

您无法在after_挂钩中调用save,因为您将导致文档在无限循环中触发回调。您需要使用不会触发回调的内容,例如update_attribute。

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

答案 1 :(得分:0)

您应该在gdoc_key

中设置before_create
before_create :set_gdoc_key
def set_gdoc_key
  self.gdoc_key = 'qwerty'
end
相关问题