2013-12-09 67 views
1

這是我的模型:mongoid after_remove回調不工作

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

我認爲你是從數據庫中摧毀城市。您應該從關係中刪除它:

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