我已经构建了一个表单,允许用户在我们的应用上设置店面。它工作正常。
我现在正尝试添加将问题与产品相关联的功能。并非每个问题都适用于每个产品。例如,如果店面正在销售T恤,那么适用的问题可能是"您的尺寸是多少?"。
以下是我的模型的基本图表:
以下是我的模型类中的相关行:
class Organization < ActiveRecord::Base
has_one :storefront
has_many :questions
end
class Storefront < ActiveRecord::Base
belongs_to :organization
has_many :questions, through: :organizations
has_many :products, class_name: Product::Base, dependent: :destroy
end
class Product::Base < ActiveRecord::Base
belongs_to :storefront
has_many :product_question_applicabilities
end
class ProductQuestionApplicability < ActiveRecord::Base
belongs_to :product, inverse_of: :product_question_applicabilities
belongs_to :question, inverse_of: :product_question_applicabilities
end
class Question < ActiveRecord::Base
belongs_to :organization
has_many :answers
end
我无法设置表单以接受新问题模型的属性。
这是我的表单的样子(剥离到相关的行):
<%= form_for @storefront, url: create_or_edit_storefront_path(@storefront) do |f| %>
<%= f.fields_for :questions do |ff| %>
<%= render 'question_fields', f: ff %>
<% end %>
<%= link_to_add_association 'add question', f, :questions, :class => "add-question new-thing flat-link" %>
<% end %>
这给了我一个错误&#34; Storefronts中的ActiveRecord :: HasManyThroughAssociationNotFoundError#edit,找不到关联:模型Storefront&#34;
中的组织我是否正确设置了模型以实现此目的?
答案 0 :(得分:0)
您需要将accepts_nested_attributes_for添加到您的模型中
尝试更改
class Storefront < ActiveRecord::Base
belongs_to :organization
has_many :questions, through: :organizations
has_many :products, class_name: Product::Base, dependent: :destroy
end
到
class Storefront < ActiveRecord::Base
belongs_to :organization
has_many :questions, through: :organizations
has_many :products, class_name: Product::Base, dependent: :destroy
accepts_nested_attributes_for :questions
end