Ruby-on-rails 在 Rails 中找不到关联问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1781202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not find the association problem in Rails
提问by Ash
I am fairly new to Ruby on Rails, and I clearly have an active record association problem, but I can't solve it on my own.
我是 Ruby on Rails 的新手,我显然有一个活动记录关联问题,但我无法自己解决。
Given the three model classes with their associations:
给定三个模型类及其关联:
# application_form.rb
class ApplicationForm < ActiveRecord::Base
has_many :questions, :through => :form_questions
end
# question.rb
class Question < ActiveRecord::Base
belongs_to :section
has_many :application_forms, :through => :form_questions
end
# form_question.rb
class FormQuestion < ActiveRecord::Base
belongs_to :question
belongs_to :application_form
belongs_to :question_type
has_many :answers, :through => :form_question_answers
end
But when I execute the controller to add questions to application forms, I get the error:
但是当我执行控制器向申请表添加问题时,出现错误:
ActiveRecord::HasManyThroughAssociationNotFoundError in Application_forms#show
Showing app/views/application_forms/show.html.erb where line #9 raised:
Could not find the association :form_questions in model ApplicationForm
Can anyone point out what I am doing wrong?
谁能指出我做错了什么?
回答by Jim
In the ApplicationForm class, you need to specify ApplicationForms's relationship to 'form_questions'. It doesn't know about it yet. Anywhere you use the :through, you need to tell it where to find that record first. Same problem with your other classes.
在ApplicationForm 类中,您需要指定ApplicationForms 与'form_questions' 的关系。它还不知道。:through无论在何处使用,都需要先告诉它在哪里可以找到该记录。你的其他班级也有同样的问题。
So
所以
# application_form.rb
class ApplicationForm < ActiveRecord::Base
has_many :form_questions
has_many :questions, :through => :form_questions
end
# question.rb
class Question < ActiveRecord::Base
belongs_to :section
has_many :form_questions
has_many :application_forms, :through => :form_questions
end
# form_question.rb
class FormQuestion < ActiveRecord::Base
belongs_to :question
belongs_to :application_form
belongs_to :question_type
has_many :form_questions_answers
has_many :answers, :through => :form_question_answers
end
That is assuming that's how you have it set up.
那是假设这就是您设置的方式。
回答by bensie
You need to include a
你需要包括一个
has_many :form_question_answers
In your FormQuestion model. The :through expects a table that's already been declared in the model.
在您的 FormQuestion 模型中。:through 需要一个已经在模型中声明的表。
Same goes for your other models -- you can't supply a has_many :throughassociation until you've first declared the has_many
您的其他模型也是如此——has_many :through在您首先声明has_many
# application_form.rb
class ApplicationForm < ActiveRecord::Base
has_many :form_questions
has_many :questions, :through => :form_questions
end
# question.rb
class Question < ActiveRecord::Base
belongs_to :section
has_many :form_questions
has_many :application_forms, :through => :form_questions
end
# form_question.rb
class FormQuestion < ActiveRecord::Base
belongs_to :question
belongs_to :application_form
belongs_to :question_type
has_many :form_question_answers
has_many :answers, :through => :form_question_answers
end
It looks like your schema might be a bit wonky, but the point is you always need to add the has_many for the join table first, then add the through.
看起来您的架构可能有点不稳定,但关键是您始终需要先为连接表添加 has_many,然后添加通过。

