Viele-zu-Viele-zu-Viele-Beziehung mit der Notwendigkeit eines anderen spezifischen Modells
Ich habe eine viele-zu-viele Beziehung zwischenSupermarkt, Produkt undMarke durch dieLiefern- undUrsprung-Modelle. Ich möchte auch speichern, welche spezifische Produkt-Marken-Kombination ich in meinem Supermarkt habe. Ich dachte an ein anderes Modell (ich nannte esSpecific_Combination
wo ich lagern würde:supermarket_id
, :product_id
und: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
Und jetzt die Klasse, von der ich dachte, dass ich sie verwenden könnte, um eine bestimmte Produkt-Marken-Kombination zu speichern
class Specific_Combination < ActiveRecord::Base
# to show which columns I would use:
attr_accessible :supermarket_id, :product_id, :brand_id
end
Ist das ein geeigneter Ansatz?Wie muss ich die Beziehungen zu und von modellierenSpecific_Combination
?Wie würde ich auf die Artikel in zugreifen (erstellen ...)?Specific_Combination
?Wie würde ein besserer Ansatz (Normalisierung) aussehen?Bearbeiten
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
/Bearbeiten