从表中删除?

时间:2011-04-18 07:16:14

标签: ruby-on-rails

我仍然有以下错误:我想删除问题,但我明白了:

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

我坚持这个:(

2 个答案:

答案 0 :(得分:1)

你不能拥有:dependent =&gt; :销毁问题模型,因为它是查询的子对象。这就是调用导致问题的查询的原因。

答案 1 :(得分:0)

看起来belongs_to :inquiry , :dependent => :destroy应为belongs_to :inquiry 在查询模型中,它应该是has_one :question, :dependent => :destroy

你真的不能拥有:dependent =&gt; :销毁子对象