我想了解导轨协会请解释导轨has_many通过协会
我有下面的表,我已经定义了他们的关系,任何一个请帮我理解。 这些表格是产品,产品销售商和分销商。 每个产品都有一个分销商,经销商进行多个产品
我定义这些作为
class Product < ActiveRecord::Base
has_one :product_distributor
has_one :distributor, through: :product_distributor
end
class ProductDistributor < ActiveRecord::Base
belongs_to :products
belongs_to :distributors
end
class Distributor < ActiveRecord::Base
has_many :product_distributors
has_many :products, through: :product_distributors
end
这是正确的吗?如果不是,我该如何纠正它?
感谢Vinod的回答。 “经销商”类名是我在stackoverflow中犯的一个错字。对于那个很抱歉。 – user2056101
@ user2056101,发生了!我认为你仍然需要修正'belongs_to'声明,你的'belongs_to:products'和'belongs_to:distributor'应该是单数而不是复数形式的答案。您可以忽略'class_name',因为您已更正模型名称。 – vee