Ruby on rails 删除行时出现问题

Ruby on rails 删除行时出现问题,ruby-on-rails,Ruby On Rails,嗯,当我想删除行时,我第一次看到这个:(我想删除答辩人的电子邮件)我得到了他的: Mysql::Error: Cannot delete or update a parent row: a foreign key constraint fails (`survey_development`.`inquiries`, CONSTRAINT `inquiries_ibfk_2` FOREIGN KEY (`respondent_id`) REFERENCES `respondents` (`id`)

嗯,当我想删除行时,我第一次看到这个:(我想删除答辩人的电子邮件)我得到了他的:

Mysql::Error: Cannot delete or update a parent row: a foreign key constraint fails (`survey_development`.`inquiries`, CONSTRAINT `inquiries_ibfk_2` FOREIGN KEY (`respondent_id`) REFERENCES `respondents` (`id`)): DELETE FROM `respondents` WHERE `id` = 4
p、

用户模型:

has_many :inquiries 
has_many :questions, :through => :inquiries 
has_many :answers, :through => :inquiries
问题模式:

has_many :inquiries, :dependent => :destroy 
has_many :answers, :through => :inquiries, :dependent => :destroy 
应答模型

  belongs_to :inquiry
  belongs_to :question
查询模型

  belongs_to  :question
  belongs_to  :users
  has_one    :answer, :dependent => :destroy
主控器

  # DELETE /respondents/1
  def destroy
    @respondent.destroy
    head :ok
  end
应答器模型

class Respondent < ActiveRecord::Base
  has_many :inquiries
  has_many :questions, :through => :inquiries
  has_one :answer,   :through => :inquiry
end
类响应者:询问
有一个:答案,:通过=>:询问
结束
让我们开始吧

首先,这是错误的代码:

class Respondent < ActiveRecord::Base
  has_many :inquiries
  has_many :questions, :through => :inquiries
  has_one :answer,   :through => :inquiry
end
在您的
查询中
模型

第三,你不能用这个

has_one :answer,   :through => :inquiry
只要未定义关联
查询

所以你应该

  • 受访者id
    添加到您的
    查询
    表中
  • 所属\u添加到您的
    查询
    模型中:应答者
    关联
  • 删除
    有一个:答案,:通过=>:inquiry
    来自
    受访者的关联
    模型。或者找出它是什么,并添加一些新的关联来修复它

你的问题不是一个坏代码。你的问题是你不明白自己在做什么。你应该感受Rails,你应该热爱Rails上帝保佑你


另外,您的
答案表中没有
问题id

那么我们如何知道您删除行的方式呢?显示您的代码OK。下一个问题。我们如何知道您获得受访者的方式?显示与您的案例相关的所有代码,manadded,我希望您是认真的?您列出了所有模型,而不是您正在处理的确切内容。Show
responder
modelLOOl,我是新手,对不起,等一下,我马上就来!m、 仍然不起作用,仍然是相同的错误,但几分钟前我不能编辑用户,但现在我可以,但不能删除。您的代码中有很多问题。(我只看到那20行)。您的数据库中没有用于关联的外键。例如,如果您说:
答案属于:问题
,但您没有将
问题id
引用添加到您的
答案
表中,您应该将
查询有多个:响应
添加到您的查询模型中,并且您应该将引用键
响应者id
添加到其表中,以此类推。。。伙计,你一定会读到联想的
has_many :respondents
has_one :answer,   :through => :inquiry