Ruby on rails 多对多对多关系,需要另一种特定模型

Ruby on rails 多对多对多关系,需要另一种特定模型,ruby-on-rails,database-design,activerecord,many-to-many,Ruby On Rails,Database Design,Activerecord,Many To Many,我通过供应和原产地模型在超市、产品和品牌之间建立了多对多关系。 我还想在我的超市里储存我的特定产品品牌组合。 我想到了另一种模式(我称之为特定组合,我将在那里存储:超市id,:产品id和:品牌id) class Supermarket < ActiveRecord::Base has_many :supplies has_many :products, :through => :supplies end class Supply < ActiveRecord::Bas

我通过供应和原产地模型在超市产品品牌之间建立了多对多关系。 我还想在我的超市里储存我的特定产品品牌组合。 我想到了另一种模式(我称之为
特定组合
,我将在那里存储
:超市id
:产品id
:品牌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

/Edit

在这里,我可能有来自原产地的供应,而不是产品

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
此外,请考虑是否需要特定组合。您将对其执行哪些操作?是否要列出数据库中的所有特定组合?是否要搜索以查找特定组合?是否要创建新组合

然后想想这些操作是否可以简单地用其他类来完成

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)
class超市:供应
自我选择
Supermarket.all.map{| Supermarket |[Supermarket.name,Supermarket.id]}
结束
结束
结束
类提供“沃尔玛”);
玉米片=产品。创建(:name=>“玉米片”);
kellogs=Brand.create(:name=>“Kellog's”);
walmart.origins.create(:product\u id=>cornflakes,:brand\u id=kellogs)

啊,好吧,我明白了:
特定组合
是不必要的,我
可以有
origin\u id`在供应中。但是我如何在供应中创建一个新条目呢?我不是指一个新列,而是指一个新的数据库条目(
Supermarket.create(:name=>'Walmart')。origin.create(“”)
)?Walmart=Supermarket.create(:name=>“Walmart”);玉米片=产品。创建(:name=>“玉米片”);kellogs=品牌。创建(:name=>“Kellog's”);沃尔玛。起源。创建(:Product\u id=>玉米片,:Brand\u id=kellogs)很好,这就是我一直在寻找的解决方案。很抱歉在我的上一个问题中没有清楚地表达出来。附加问题:如何通过中的下拉菜单访问
Supply
-模型?通常,我在ActiveRecord类中有一个choices类方法,它返回一个[name,id]数组数组。我可以将其传递给form.select以创建下拉框。我建议在Supply类中使用choices类方法,该方法为下拉框返回一个选项数组,该下拉框是要传递给select方法的[description,id]数组数组,并设置描述格式,但您希望以何种方式显示超市、原产地组合。(如果需要,对于原始模型也是如此。)很抱歉再次打扰您……但是我还没有弄清楚如何从choice模型类访问变量。我认为应该是这样的:
def self.to_be_selected_supermarket=Array.new supermarket.find_each do | supermarket | selected_supermarket我没有获得Originations表。一种产品有许多品牌起源?我甚至不知道这意味着什么。一种产品属于一个品牌。我会将供应更改为库存,并添加一个数量字段。也许
产品
不是我这里的意思的最佳描述。我可能应该称之为
类别
-就像可乐可以来自可口可乐或百事可乐一样。。。
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)