2011-04-15 59 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