2011-04-01 18 views
0

我不得不通過視頻和主題之間的關聯一個的has_many,與topicable作爲獨立資源。我想有使只有topicable協會被刪除,而不是視頻,也不是主題的鏈接。這是我迄今所做的:爲什麼這會刪除視頻資源?

在我的routes.rb:

resource :topicable, :only => :destroy 

在我topicables控制器:

def destroy 
    @topicable = Topicable.find(params[:id]) 
    @topicable.destroy 
    respond_to do |format| 
     format.html {redirect_to @video} 
     format.js 
    end 
    end 

最後,在我的視頻節目的看法:

<%= link_to "x", @topicable, :method => :delete, :class => 'topic_delete' %> 

這樣就刪除了相關聯,而且視頻......這不是我想做的事情。我怎樣才能解決這個問題?

UPDATE:

這是我的視頻模式:

has_many :topicables, :dependent => :destroy 
has_many :topics, :through => :topicables 

這是所有的在topicable模式:

belongs_to :video 
belongs_to :topic 

注意:這似乎並不有什麼做:依賴=>:摧毀

我有這個在我的視頻模式分配主題作爲虛擬屬性。這可以幫助:

attr_accessor :topic_names 
after_save :assign_topics 

def assign_topics 
    if @topic_names 
    self.topics << @topic_names.map do |name| 
     Topic.find_or_create_by_name(name) 
    end 
    end 
end 
+0

顯示Topicable模型 – fl00r 2011-04-01 19:01:03

+0

和你topicable模式? – 2011-04-01 19:02:07

+0

你還沒有分化'@ video'來重定向 – fl00r 2011-04-01 19:03:50

回答

-1

看起來你Topicable模式已經得到了這條線

belongs_to :video, :dependent => :destroy 

將其替換爲

belongs_to :video 
+0

不,這是不是這樣的... ... - – 2011-04-01 19:18:45

-1

根據您的協會是如何設置的TopicableVideo之間確保你沒有任何:dependent => :destroy選項設置。

如果你有has_many設置爲

class Whatever 
    has_many :something_elses, :dependent => :nullify 
end 

這樣的外鍵將被撤銷,對象不會被破壞

+0

這會進入哪一類?我真的不認爲這是什麼導致這 – 2011-04-01 19:26:51