我在理解整体概念及其在 Rails 中的工作原理方面遇到了一些困难,我认为它可以为您完成一些工作,因为我习惯于手动构建关联。我目前有一个计费系统,我正在尝试创建一个订单部分,显然我的订单必须有许多产品,因此我已将数据库标准化为第三范式,以便订单和产品通过另一个链接名为订购产品的表,该表包含订单 ID 和产品 ID,以及订购产品的数量。我根据我所知道的 has_Many :through 通过 Code School 教程在 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
我添加了源属性,因为我在控制台中收到错误,建议我应该将源属性添加到 has_many :through 行。这是当我尝试将产品添加到订单时在控制台中收到的错误:
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>'
最佳答案
在本例中,它是product=方法;订单没有“产品”,它有很多“产品”。
你可以:
order.products << some_product
不幸的是,CamelCase 也是一个问题。 Order 和 Product 的关联将分别查找 order_id 或 Product_id,但不会找到它们。解决方案是将列名称更改为 Snake_case,或添加 :foreign_key => ... 选项。
关于mysql - 导轨 : Trouble with has many through relationships,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16497604/