【问题标题】:Association where client orders from products. How to setup relation客户从产品订购的关联。如何设置关系
【发布时间】:2014-05-18 08:24:10
【问题描述】:

我正在摆弄 Rails 并尝试构建一个用于练习目的的小应用程序:

我希望客户订购一种或多种产品

我有一个client 表,一个product 表,最后一个order 表有一个client_idproduct_id

现在,我不太确定如何在这些表之间建立良好的关系,例如:客户进入产品页面,选择产品并保存订单。

非常感谢任何关于哪个模型应该具有哪个关系的帮助。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 associations


    【解决方案1】:

    你可以像这样设置关联

    Class Client < ActiveRecord::Base
    
    has_many :orders
    has_many :products,through: :orders 
    
    end
    
    Class Product < ActiveRecord::Base
    
    has_many :orders
    has_many :clients,through: :orders
    
    end
    
    Class Order < ActiveRecord::Base
    
    belongs_to :client
    belongs_to :product
    
    end
    

    有关详细信息,请参阅这些Guides

    【讨论】:

    • 像魅力一样工作。谢谢帕万!
    • 我没有投反对票,但我认为这是因为答案中缺乏上下文
    • 产品和客户与订单有直接关系,使用through 没有意义
    • @RSB 你是对的!我想不出我是怎么错过的。谢谢,你得到了我的支持 :)
    • 很高兴提供帮助,现在是正确的,现在应该删除不赞成票
    【解决方案2】:

    关联应该如下所示

    Class Client < ActiveRecord::Base
    
      has_many :orders
      has_many :products,through: :orders
    
    end
    
    Class Product < ActiveRecord::Base
    
      has_many :orders
      has_many :clients,through: :orders
    
    end
    
    Class Order < ActiveRecord::Base
    
      belongs_to :client
      belongs_to :product
    
    end
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      #app/models/product.rb
      has_many :orders
      has_many :clients, through: :orders
      
      #app/models/order.rb
      belongs_to :client
      
      has_many :order_products
      has_many :products, through: :order_products
      
      #app/models/client.rb
      has_many :orders 
      has_many :products, through: :orders
      

      处理创建新order 的方法是为其设置uuid,然后创建另一个处理订单产品的连接模型。你可以使用order 模型来做到这一点,但我觉得最好描述基本方式,因为它会给你一些工作的机会

      --

      uuid

      我们喜欢为订单等敏感数据设置uuid 列:

      #migration
      add_column :orders, :uuid, :varchar, after: :id
      
      #app/models/order.rb
      before_create :set_uuid
      
      private
      
      def set_uuid
         unless self.uuid
             loop do
                 token = SecureRandom.hex(5)
                 break token unless self.class.exists?(uuid: token)
             end
         end
      end
      

      这意味着每个订单都会有您想要的任意数量的产品,可以这样访问:

      @user.orders.first.products #-> returns order products
      

      --

      编辑 - 刚刚看到@Pavan's 的回答。对不起,如果它是一样的 - 希望它有帮助!

      【讨论】:

        【解决方案4】:

        你有两种方法

        1) 在客户端和产品模型之间设置 has_and_belongs_to_many。

        class Client < ActiveRecord::Base
          has_and_belongs_to_many :products
        end
        
        class Product < ActiveRecord::Base
          has_and_belongs_to_many :clients
        end
        

        2) 通过使用关键字 through 设置客户端和生产之间的关系

        class Client < ActiveRecord::Base
          has_many :orders
          ha_smany :products, through: orders
        end
        
        class Product < ActiveRecord::Base
          has_many :orders
          ha_smany :clients, through: orders
        end
        

        我建议您使用第二个选项,因为您有一个中间模型。

        【讨论】:

        • 除了错字我没有得到答案(订单模型在哪里?!)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-26
        • 2015-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多