【发布时间】:2012-09-08 20:49:10
【问题描述】:
我通过供应在超市、产品和品牌之间建立了多对多的关系-和 Origin-模型。
我还想存储我在超市中拥有的特定产品-品牌-组合。
我想到了另一个模型(我称之为Specific_Combination,我将在其中存储:supermarket_id、:product_id 和:brand_id。
class Supermarket < ActiveRecord::Base
has_many :supplies
has_many :products, :through => :supplies
end
class Supply < ActiveRecord::Base
belongs_to :product
belongs_to :supermarket
end
class Product < ActiveRecord::Base
has_many :supplies
has_many :supermarkets, :through => :supplies
has_many :origins
has_many :brands, :through => :origins
end
class Origin < ActiveRecord::Base
belongs_to :products
belongs_to :brands
end
class Brand < ActiveRecord::Base
has_many :origins
has_many :products, :through => :origins
end
现在我认为可以用来存储特定产品-品牌组合的类
class Specific_Combination < ActiveRecord::Base
# to show which columns I would use:
attr_accessible :supermarket_id, :product_id, :brand_id
end
- 这是一种合适的方法吗?
- 如何对与
Specific_Combination之间的关系建模? - 如何访问(创建...)
Specific_Combination中的项目? - 更好的方法(标准化)应该是什么样子?
编辑
class Supply < ActiveRecord::Base
belongs_to :origin
belongs_to :supermarket
end
class Product < ActiveRecord::Base
has_many :origins
end
class Origin < ActiveRecord::Base
belongs_to :product
belongs_to :brands
end
class Brand < ActiveRecord::Base
has_many :origins
end
class Supermarket < ActiveRecord::Base
has_many :supplies
has_many :origins, :through => :supplies
# my attempt to create an array of names of supermarkets
def self.to_be_chosen
chosen_supermarket = Array.new
Supermarket.find_each do |supermarket|
chosen_supermarket << supermarket.name
end
return chosen_supermarket
end
end
/编辑
【问题讨论】:
-
我没有得到 Origins 表。一个产品有多个品牌来源?我什至不知道那是什么意思。一个产品属于一个品牌。我会将供应更改为库存并添加一个数量字段
-
也许
Product不是我在这里的意思的最佳描述。我可能应该称它为Category- 就像可乐可以来自可口可乐或百事可乐......
标签: ruby-on-rails database-design activerecord many-to-many