Mongoid:has_and_belongs_to_many分配不会触发after_save

时间:2014-09-02 08:06:42

标签: mongoid

我有下一个代码:

def Person
  has_and_belongs_to_many :houses

  def after_save do
    p "After save"
  end
end

def House
  has_and_belongs_to_many :persons
end

p = Person.create
h = House.create
p.house_ids
=> nil

p.houses << h  # it should print 'After save'
p.house_ids
=> ["540591aad9a0d13cee00003e"] 

它应该打印“保存后”,但它不会触发回调,为什么不呢?

1 个答案:

答案 0 :(得分:0)

因为after_save在创建对象时没有被触发...使用after_create回调并且它会正确打印