2012-04-04 72 views
0

我的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 %> 
+0

您可能需要':dependent =>:destroy'關聯 例如:'has_many:products,:through =>:products_tracks,:dependent =>:destroy' – VelLes 2012-04-04 15:47:15

+0

會給出一個答案,謝謝。 – Raoot 2012-04-04 15:52:18

+0

害怕,不解決它。 – Raoot 2012-04-04 15:59:15

回答

1

首先,您需要爲您的協會:dependent => :destroy選項:

class Track < ActiveRecord::Base 
    has_many :releases_tracks, :dependent => :destroy 
    has_many :releases, :through => :releases_tracks # also note you had here :tracks instead of :releases 

    has_many :products_tracks, :dependent => :destroy 
    has_many :products, :through => :products_tracks 
end 

而且,如果你還想要刪除曲目刪除版本或產品時,添加以下:dependent => :destroy S:

class Release < ActiveRecord::Base 
    has_many :releases_tracks, :dependent => :destroy 
    has_many :tracks, :through => :releases_tracks 
end 

class Product < ActiveRecord::Base 
    has_many :products_tracks, :dependent => :destroy 
    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, :dependent => :destroy 
end 

class ReleasesTrack < ActiveRecord::Base 
    belongs_to :release 
    belongs_to :track, :dependent => :destroy 
end 
+0

謝謝,現在終於繼續前進!我已經添加了:dependent =>:在我的Tracks模型中指出了銷燬,現在關聯隨着軌道一起被刪除,但是,我想只刪除關聯並將軌道留在數據庫中。 – Raoot 2012-04-04 16:30:47

+0

仍然無法刪除這裏的關聯,任何人都可以提供一些指導嗎? – Raoot 2012-04-05 11:15:23

+0

你能否更清楚地解釋你的問題?你能提供你正在嘗試做什麼的代碼嗎? – 2012-04-12 11:49:28

相關問題