2011-04-15 69 views
0

我有4個型號。我想刪除問題,但現在我不能。不知道爲什麼知道。我想,首先,我需要刪除這個問題的答案,然後刪除查詢,然後問題本身。對。但我怎麼能做到這一點?通過關係表刪除東西

有我的模型:

-respondents_model

class Respondent < ActiveRecord::Base 
    has_many :inquiries 
    has_many :questions, :through => :inquiries 
    has_many :answers, :through => :inquiries 
end 

-answer_model

class Answer < ActiveRecord::Base 
    belongs_to :inquiry 
    belongs_to :question 

    validates_uniqueness_of :inquiry_id 
end 

-question_model

class Question < ActiveRecord::Base 
    has_one :answer, :through => :inquiry , :dependent => :destroy 
    belongs_to :inquiry , :dependent => :destroy 
end 

-inquiry_model

class Inquiry < ActiveRecord::Base 
    belongs_to :question 
    belongs_to :respondent 
    has_one :answer 
end 

和我question_controller

def destroy 
    @question.destroy 
    head :ok 
    end 

回答

0

您不必刪除答案,因爲他們將盡可能設置:dependent => :destroy被自動刪除。所以,你只需要調用:

還需要指定什麼確切的問題,你要毀滅:Question.find params[:id]

def destroy 
    @question = Question.find params[:id] 
    @question.destroy 
    head :ok 
end 
+0

感謝響應快!但它不起作用:我不知道爲什麼。在控制檯中:SELECT * FROM'questions' WHERE('questions'.'id' = 0).. hmm – 2011-04-15 11:03:52

+0

什麼不起作用?你有什麼想要做的? – fl00r 2011-04-15 11:07:20

+0

我試圖刪除問題...我在控制器中寫了什麼,你給我..還是一樣;(也許我錯過了一些東西? – 2011-04-15 11:08:21