许多一对多到许多有需要另一种特定的模式的关系 [英] Many-to-Many-to-Many relationship with need for another specific model

查看:143
本文介绍了许多一对多到许多有需要另一种特定的模式的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之间有很多一对多的关系的超市 产品品牌通过的供应的 - 和原产地的-models。 我也想存储哪些特定产品,品牌组合我在我的超市。 我想到另一个模型的(我把它叫做 Specific_Combination 在那里我将存储:s​​upermarket_​​id : PRODUCT_ID :brand_id

I have a many-to-many relationship between Supermarket, Product and Brand through the Supply- and Origin-models. I also want to store which specific Product-Brand-Combination I have in my supermarket. I thought of another model (I called it Specific_Combination where I would store :supermarket_id, :product_id and :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

而现在我想我可以用它来存储一个特定的产品,品牌组合类

And now the class I thought i could use to store a specific Product-Brand-combination

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
  • 项目
  • 如何将一个更好的方法(标准化)是什么样子?
    • Is this a suitable approach?
    • How do I have to model the relationships to and from Specific_Combination?
    • How would I access (create...) the items in Specific_Combination?
    • How would a better approach (normalization) look like?
    • 修改

      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
      

      /修改

      推荐答案

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

      Here, I might have supply belong to origin instead of product.

      此外,考虑一下你是否需要SpecificCombination。你打算什么作业做了吗?你要列出数据库中所有SpecificCombinations?你要搜索找到特定的一个?你要创建一个新的组合?

      Also, think about whether you need SpecificCombination. What operations are you going to do with it? Are you going to list all the SpecificCombinations in your database? Are you going to search to find a specific one? Are you going to create a new combination?

      后来想想,如果这些操作可以简单地与其他类来完成?

      Then think if these operations can be done simply with the other classes?

      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)
      

      这篇关于许多一对多到许多有需要另一种特定的模式的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆