Проблема с удалением строки - PullRequest
0 голосов
/ 21 апреля 2011

Хм, Первый раз, когда я вижу это, когда я хочу удалить строку: (Я хочу удалить респондент.email) я получил его:

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.s

users (table): id, email 
questions (table): id, text 
inquiries: question_id, user_id 
answers: inquiry_id, text

Модель пользователей:

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

respondents_controller

  # DELETE /respondents/1
  def destroy
    @respondent.destroy
    head :ok
  end

respondent_model

class Respondent < ActiveRecord::Base
  has_many :inquiries
  has_many :questions, :through => :inquiries
  has_one :answer,   :through => :inquiry
end

1 Ответ

0 голосов
/ 21 апреля 2011

Давайте начнем.

Первое - это неправильный код:

class Respondent < ActiveRecord::Base
  has_many :inquiries
  has_many :questions, :through => :inquiries
  has_one :answer,   :through => :inquiry
end

Во-вторых, у вас нет respondent_id в таблице inquiries, и вы должны определить

has_many :respondents

в вашей Inquiry модели.

В-третьих, вы не можете использовать это

has_one :answer,   :through => :inquiry

насколько ассоциация inquiry не определена.

Итак, вы должны

  • добавить respondent_id к вашему inquiries столу
  • добавить belongs_to :respondent ассоциацию к вашей Inquiry модели
  • удалить has_one :answer, :through => :inquiry ассоциацию из Respondent модели. Или выясните, что это, и добавьте новую ассоциацию, чтобы исправить это.

И почитайте несколько статей об ассоциациях Rails .

Ваша проблема не плохой код. Ваша проблема в том, что вы не понимаете, что делаете. Вы должны чувствовать Rails, и вы должны любить Rails. Благословит вас Бог

Также у вас нет question_id в вашей таблице answers.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...