我的has_many:通過發行版/產品/曲目上的關聯似乎刪除了跟蹤並在releases_tracks/products_tracks表中刪除了孤立的關聯。我看不出我出錯的地方,我認爲默認行爲是隻刪除關聯。任何人都可以幫忙嗎?爲什麼我的has_many:通過關聯在刪除時不能正常工作?
我的模型:
class Track < ActiveRecord::Base
has_many :releases_tracks
has_many :tracks, :through => :releases_tracks
has_many :products_tracks
has_many :products, :through => :products_tracks
end
class Release < ActiveRecord::Base
has_many :releases_tracks
has_many :tracks, :through => :releases_tracks
end
class Product < ActiveRecord::Base
has_many :products_tracks
has_many :tracks, :through => :products_tracks
before_save do
self.track_ids = self.releases_track_ids
end
end
class ProductsTrack < ActiveRecord::Base
belongs_to :product
belongs_to :track
end
class ReleasesTrack < ActiveRecord::Base
belongs_to :release
belongs_to :track
end
我的軌道控制(用於摧毀行動):
class TracksController < ApplicationController
before_filter :get_track_parent
def destroy
@track = @parent.tracks.find(params[:id])
@track.destroy
redirect_to @parent
end
private
def get_track_parent
if params[:product_id].present?
@parent = Product.find(params[:product_id])
elsif params[:release_id].present?
@parent = Release.find(params[:release_id])
end
end
end
我消滅在發佈鏈接查看:
<%= link_to image_tag("icons/delete.png"), release_track_path(@release,track), :confirm => 'Are you sure?', :method => :delete %>
最後,我銷燬產品視圖中的鏈接:
<%= link_to image_tag("icons/delete.png"), product_track_path(@product,track), :confirm => 'Are you sure?', :method => :delete %>
您可能需要':dependent =>:destroy'關聯 例如:'has_many:products,:through =>:products_tracks,:dependent =>:destroy' – VelLes 2012-04-04 15:47:15
會給出一個答案,謝謝。 – Raoot 2012-04-04 15:52:18
害怕,不解決它。 – Raoot 2012-04-04 15:59:15