我想知道一个好的做法是否通过超过2个表创建一个has_many。例如:
我有
class Color < ActiveRecord::Base
attr_accessible :name, :hex
has_many :colorships
has_many :products, :through => :orderships
has_many :orders, :through => :orderships
accepts_nested_attributes_for :products, :orders
end
class Size < ActiveRecord::Base
# attr_accessible :title, :body
attr_accessible :size
# has_many :orders
has_many :orderships
has_many :products, :through => :orderships
has_many :orders, :through => :orderships
accepts_nested_attributes_for :products, :orders
end
class Condition < ActiveRecord::Base
attr_accessible :name, :condition
has_many :products
has_many :orderships
has_many :products, :through => :orderships
has_many :orders, :through => :orderships
accepts_nested_attributes_for :products, :orders
end
class Brand < ActiveRecord::Base
attr_accessible :name
has_many :orderships
has_many :products, :through => :orderships
has_many :orders, :through => :orderships
accepts_nested_attributes_for :products, :orders
end
大部分示例仅显示2个模型关系
class Material < ActiveRecord::Base
attr_accessible :name
has_many :orderships
has_many :products, :through => :orderships
has_many :orders, :through => :orderships
accepts_nested_attributes_for :products, :orders
end
class Order < ActiveRecord::Base
has_many :orderships
has_many :colors, :through => :orderships
has_many :sizes, :through => :orderships
has_many :materials, :through => :orderships
has_many :brands, :through => :orderships
has_many :conditions, :through => :orderships
end
所以,似乎这些模型过于严重。
class Product < ActiveRecord::Base
has_many :orderships
has_many :colors, :through => :orderships
has_many :sizes, :through => :orderships
has_many :materials, :through => :orderships
has_many :brands, :through => :orderships
has_many :conditions, :through => :orderships
end
与关系,但在皮革干燥模型
class Ordership < ActiveRecord::Base
# attr_accessible :title, :body
attr_accessible :product_id, :size_id, :order_id,:condition_id,:brand_id,:material_id
#
belongs_to :product
belongs_to :color
belongs_to :order
belongs_to:condition
belongs_to:material
belongs_to:Brand
end