我正在开发Agile RoR的软件仓库教程。 我已经看了一会儿,没有看到错误。我错过了什么? 我将商品添加到购物车时出现以下错误
我已经进行了迁移。
.. /Users/computername/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:376:in
method_missing' /Users/computername/Documents/rails_projects/depot/app/models/cart.rb:5:in
add_product” /Users/computername/Documents/rails_projects/depot/app/controllers/line_items_controller.rb:46:in `创建'
这是我的创建方法
def create
@cart = find_or_create_cart
product = Product.find(params[:product_id])
#@line_item = @cart.line_items.build(:product => product)
@line_item = @cart.add_product(product.id)
..
我的购物车型号
class Cart < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
def add_product(product_id)
current_item = line_items.where(:product_id => product_id).first
if current_item
current_item.quantity += 1
else
current_item = LineItem.new(:product_id=>product_id)
line_items << current_item
end
current_item
end
end
答案 0 :(得分:3)
使用conditions
代替where
line_items.conditions(:product_id => product_id).first
答案 1 :(得分:3)
where
是在ActiveRecord 3中引入的。所以在你的情况下失败是正常的,因为你使用的是ActiveRecord 2.3.x