我有以下的模型關聯:before_destroy和依賴銷燬不點火
class Slider < ActiveRecord::Base
has_one :featured_happening, :as => :featured_item, :dependent => :destroy
before_destroy :destroy_featured_happening
after_create :create_featured_happening
end
class FeaturedHappening < ActiveRecord::Base
belongs_to :featured_item, :polymorphic => true
end
當我破壞Slider
對象我以爲dependent => :destroy
會自動銷燬的featured_item
但事實並非如此。
滑動控制器
def destroy
slider = Slider.find(params[:id])
slider.delete
render(:status => :ok, :nothing => true)
end
於是我嘗試了回調與before_destroy
手動刪除featured_item
當滑塊對象被銷燬,並沒有獲取調用。
如何刪除滑塊對象時刪除featured_item
?使用Rails 3.2。
你如何刪除'Slider'實例? – 2012-03-26 03:35:18
def destroy slider = Slider.find(params [:id]) slider.delete render(:status =>:ok,:nothing => true) end – chell 2012-03-26 04:00:53