I would like to know if is a good practice create a has_many thought with more than 2 tables. for exemple:
I have
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
The most part the exemples just show 2 models relation
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
So, seems that overblunder the models.
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
with relations but at leat dry out models
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
Aucun commentaire:
Enregistrer un commentaire