我可以通过父模型进行多次关联吗?
Can I have a has-many-through association via a parent model?
我构建了一个表单,允许用户在我们的应用程序上设置店面。它工作正常。
我现在正在尝试添加将问题与产品相关联的功能。并非每个问题都适用于每个产品。例如,如果店面正在销售 T 恤,则适用的问题可能是 "What size are you?".
这是我的模型的基本图:
这是我模型中的相关行 类:
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 %>
这给我一个错误"ActiveRecord::HasManyThroughAssociationNotFoundError in Storefronts#edit, Could not find the association :organizations in model Storefront"
我是否正确设置了模型以实现此目的?
您需要 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
我构建了一个表单,允许用户在我们的应用程序上设置店面。它工作正常。
我现在正在尝试添加将问题与产品相关联的功能。并非每个问题都适用于每个产品。例如,如果店面正在销售 T 恤,则适用的问题可能是 "What size are you?".
这是我的模型的基本图:
这是我模型中的相关行 类:
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 %>
这给我一个错误"ActiveRecord::HasManyThroughAssociationNotFoundError in Storefronts#edit, Could not find the association :organizations in model Storefront"
我是否正确设置了模型以实现此目的?
您需要 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