【问题标题】:mongoid after_remove callback not workingmongoid after_remove 回调不起作用
【发布时间】:2013-12-09 12:23:00
【问题描述】:

这是我的模型:

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 有什么问题?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 2011-06-15
      • 1970-01-01
      • 2012-02-17
      相关资源
      最近更新 更多