gpt4 book ai didi

ruby-on-rails - 在 Rails 中找不到关联问题

转载 作者:行者123 更新时间:2023-12-03 07:54:35 24 4
gpt4 key购买 nike

我是 Ruby on Rails 的新手,我显然有一个事件记录关联问题,但我无法自己解决。

给定三个模型类及其关联:

# 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

但是当我执行 Controller 向申请表添加问题时,出现错误:
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

谁能指出我做错了什么?

最佳答案

在ApplicationForm 类中,您需要指定ApplicationForms 与'form_questions' 的关系。它还不知道。在任何地方使用 :through ,您需要先告诉它在哪里可以找到该记录。你的其他类(class)也有同样的问题。

所以

# 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

那是假设这就是您设置的方式。

关于ruby-on-rails - 在 Rails 中找不到关联问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1781202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com