【问题标题】:Many-to-Many-to-Many relationship with need for another specific model需要另一个特定模型的多对多对多关系
【发布时间】: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


【解决方案1】:

在这里,我的供应可能属于原产地而不是产品。

另外,想想你是否需要SpecificCombination。你打算用它做什么操作?您要列出数据库中的所有特定组合吗?您要搜索以找到特定的吗?您要创建一个新组合吗?

然后想想这些操作是否可以通过其他类简单地完成?

class Supermarket < ActiveRecord::Base
  has_many :supplies
  has_many :origins, :through => :supplies

  def self.choice
    Supermarket.all.map { |supermarket| [ supermarket.name, supermarket.id ] }
  end
end

end

class Supply < ActiveRecord::Base  
  belongs_to :origin
  belongs_to :supermarket  
end

class Origin < ActiveRecord::Base
  belongs_to :supplies
  belongs_to :brands
end


walmart = Supermarket.create(:name => "Walmart");

cornflakes = Product.create(:name => "Corn Flakes");
kellogs = Brand.create(:name => "Kellog's");
walmart.origins.create(:product_id => cornflakes, :brand_id = kellogs)

【讨论】:

  • 啊好吧,我明白了:SpecificCombination 不是必需的,我在 Supplycould have origin_id`。但是如何在 Supply 中创建一个新条目?我指的不是新列而是新的数据库条目 (Supermarket.create(:name =&gt; 'Walmart').origin.create(''))?
  • walmart = Supermarket.create(:name => "Walmart");玉米片 = Product.create(:name => "玉米片"); kellogs = Brand.create(:name => "Kellog's"); walmart.origins.create(:product_id => 玉米片, :brand_id = kellogs)
  • 太好了,这就是我正在寻找的解决方案。很抱歉在我之前的问题中没有清楚地表达出来。 额外问题:我如何通过下拉菜单访问Supply-Model,例如this question
  • 通常,我的 ActiveRecord 类中有一个选择类方法,它返回 [name, id] 数组的数组。我可以将它传递给 form.select 以创建一个下拉框。我建议在 Supply 类中使​​用一个选择类方法,它为下拉框返回一个选择数组,该下拉框是一个 [description, id] 数组的数组,以传递给 select 方法,以及您想要呈现超市的格式描述,产地组合。 (如果需要,对于 Origin 模型也是如此。)
  • 很抱歉再次打扰您...但我还没有弄清楚如何从选择模型类中访问变量。我认为应该类似于:def self.to_be_chosen chosen_supermarket = Array.new Supermarket.find_each do |supermarket| chosen_supermarket &lt;&lt; supermarket.name end end
猜你喜欢
  • 2011-07-29
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 2019-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多