2013-05-11 111 views
0

我一直有一点难以理解整个有很多概念以及它如何在Rails中工作,我认为它为你做腿部工作,因为我习惯于手动建立关联。我目前有一个结算系统,我正在尝试创建一个订单部分,显然我的订单必须有很多产品,所以我已经将数据库规范化为第三范式,以便订单和产品通过另一个表格命名为订购产品,此表格包含订单的ID和产品的ID以及订购产品的数量。我已经根据我所了解的has_Many创建了模型:通过代码学校教程的Rails,但是我已经完全复制了它的样子,但是地雷和它们之间必定有某种区别。这是我目前拥有的模型文件:Rails:麻烦有很多通过关系

订货型号:

class Order < ActiveRecord::Base 
    attr_accessible :ClientID, :OrderTotal 
    has_many :orderedproducts 
    has_many :products, through: :orderedproducts, :source => :product 

end 

订购的产品型号:

class Orderedproduct < ActiveRecord::Base 
    attr_accessible :OrderID, :ProductID, :QuantityOrdered 
    belongs_to :order 
    belongs_to :product 
end 

产品型号:

class Product < ActiveRecord::Base 
    #This line makes these elements accessible outside of the class. 
    attr_accessible :ProductName, :ProductPrice, :ProductQuantity, :ProductSupplier 

    has_many :orderedproducts 
    has_many :orders, through: :orderedproducts, :source => :order 

    #These attributes ensure that the data entered for each element is valid and present. 
    validates_presence_of :ProductName 
    validates_presence_of :ProductPrice 
    validates_numericality_of :ProductPrice 
    validates_presence_of :ProductQuantity 
    validates_numericality_of :ProductQuantity 
    validates_presence_of :ProductSupplier 

end 

我添加源属性因为我在控制台中发现错误,提示我应该将源属性添加到h as_many:通过线路。这是我收到的控制台中的错误,当我尝试将产品添加到订单:

irb(main):017:0> order.products = Product.find(4) 
    Product Load (0.3ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 4 LIMIT 1 
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :ProductID in model Orderedproduct. Try 'has_many :products, :through => :orderedproducts, :source => <name>'. Is it one of :order or :product? 
     from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/activerecord-3.2.13/lib/active_record/reflection.rb:509:in `check_validity!' 
     from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/activerecord-3.2.13/lib/active_record/associations/association.rb:26:in `initialize' 
     from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:24:in `initialize' 
     from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/activerecord-3.2.13/lib/active_record/associations/has_many_through_association.rb:10:in `initialize' 
     from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/activerecord-3.2.13/lib/active_record/associations.rb:160:in `new' 
     from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/activerecord-3.2.13/lib/active_record/associations.rb:160:in `association' 
     from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/activerecord-3.2.13/lib/active_record/associations/builder/association.rb:51:in `block in define_writers' 
     from (irb):17 
     from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start' 
     from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start' 
     from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>' 
     from script/rails:6:in `require' 
     from script/rails:6:in `<main>' 

回答

0

由于订单的has_many产品,你从Rails中#products方法,而不是一个#product方法。

你可以做的是使用订单实例来创建一个相关OrderedProducts和产品添加到该实例。

order = Order.new 
ordered_product = order.ordered_products.build 
ordered_product.product = Product.find(4) 

与订购#product方法的问题是有,就是要集合,所以你的代码来知道你的意思是哪一个?

0

我使用的是错误的数组运算符,应该是运算符而不是=运算符,因为我在数组中添加了一个元素。

1

在这种情况下,它的产物=方法;订单没有'产品',它有很多'产品'。

您可以:

order.products << some_product 

的首字母大写,不幸的是,也是一个问题。 Order和Product的关联将分别查找order_id或product_id,并且不会找到它们。解决方案将把列名更改为snake_case,或者添加:foreign_key => ...选项。