我有下一个代码:
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"]
它应该打印“保存后”,但它不会触发回调,为什么不呢?
答案 0 :(得分:0)
因为after_save
在创建对象时没有被触发...使用after_create
回调并且它会正确打印