2011-04-18 89 views
0

我還是有以下錯誤:我想刪除的問題,但我得到這個:從表中刪除?

ActiveRecord::StatementInvalid (Mysql::Error: Cannot delete or update a parent row: a foreign key constraint fails (`survey_development`.`inquiries`, CONSTRAINT `inquiries_ibfk_1` FOREIGN KEY (`question_id`) REFERENCES `questions` (`id`)): DELETE FROM `questions` WHERE `id` = 217): 

我控制器

def destroy 
# @question.destroy 
# head :ok 
# @question = Question.find(params[:id]) 
    @question.destroy 
    head :ok 
    end 

模型

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

    validates_presence_of :text, :message => "ERROR!" 
end 

我堅持這一點: (

回答

0

看起來belongs_to :inquiry , :dependent => :destroy應該belongs_to :inquiry 和查詢模型應該是has_one :question, :dependent => :destroy

你不能真正有:依賴=>:

+0

並且這一行是正確的「has_one:answer,:through =>:inquiry,:dependent =>:destroy」? /////////我有以下查詢:belongs_to:問題 belongs_to:受訪者 has_one:answer – 2011-04-18 09:34:32

1

您不能在Question模型上擁有:dependent =>:destroy,因爲它是th查詢的子對象。這是調用導致您的問題的查詢。

+0

感謝響應銷燬子對象上。但是我從問題模型中刪除了依賴關係,但仍然在工作,而且我應該寫依賴=>:銷燬別的地方?在其他模型? – 2011-04-18 07:27:53