mongoid after_remove回调不起作用

时间:2013-12-09 12:23:00

标签: ruby-on-rails ruby-on-rails-3 mongoid

这是我的模特:

class Country
  include Mongoid::Timestamps
  has_many :cities, after_add: :show_log, after_remove: :show_log

  def show_log
    puts "this is log!"
  end
end

在控制台中:

Country.first.cities.create FactoryGirl.attributes_for(:city)
=> [.....] this is log!
Country.first.cities.first.destroy
=> true

正如您在第二个中看到的,没有记录任何内容! mongoid after_remove有什么问题?

1 个答案:

答案 0 :(得分:1)

我认为你正在从db中摧毁这座城市。你应该只从关系中删除它:

Country.first.cities.destroy( Country.first.cities.first )