1

我正在尝试创建一个似乎无法正确访问其关联模型信息的对象。在这种情况下,我的语法有什么问题以及最佳实践是什么?以下是我的代码和错误:

控制器

class LineItemsController < ApplicationController
  def new
    @line_item = LineItem.new
  end
  def create
    @line_item = LineItem.create!(params[:line_item].merge(:cart => current_cart))
    @line_item.update_attributes!(:unit_price => @line_item.item_price)
    redirect_to current_cart_url
  end
end

模型

class LineItem < ActiveRecord::Base
  attr_accessible :cart_id, :product_id, :quantity, :unit_price, :product, :cart, :color_id, :size_id, :extra_id, :color, :size, :extra
  belongs_to :cart
  belongs_to :product
  has_one :color
  has_one :size
  has_one :extra
  validates :quantity, :presence => true
  def item_price
    if size.price.nil? || size.price == 0
      if  extra.price.nil? || extra.price ==0
        product.price
      else
        product.price + extra.price
      end
    else
      if extra.price.nil? || extra.price == 0
        product.price + size.price
      else
        product.price + size.price + extra.price
      end
    end
  end
  def full_price
          unit_price * quantity
  end
end

错误

ActiveRecord::StatementInvalid at /line_items
PG::Error: ERROR:  column sizes.line_item_id does not exist
LINE 1: SELECT  "sizes".* FROM "sizes"  WHERE "sizes"."line_item_id"...
                                              ^
: SELECT  "sizes".* FROM "sizes"  WHERE "sizes"."line_item_id" = 4 LIMIT 1

ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#log
activerecord (3.2.13) lib/active_record/connection_adapters/abstract_adapter.rb, line 285
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#log
activerecord (3.2.13) lib/active_record/connection_adapters/abstract_adapter.rb, line 275
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#exec_query
activerecord (3.2.13) lib/active_record/connection_adapters/postgresql_adapter.rb, line 659
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#select
activerecord (3.2.13) lib/active_record/connection_adapters/postgresql_adapter.rb, line 1256
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#select_all
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/database_statements.rb, line 18
block in ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#select_all
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/query_cache.rb, line 61
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#cache_sql
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/query_cache.rb, line 75
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#select_all
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/query_cache.rb, line 61
block in Size.find_by_sql
activerecord (3.2.13) lib/active_record/querying.rb, line 38
Size.logging_query_plan
activerecord (3.2.13) lib/active_record/explain.rb, line 41
Size.find_by_sql
activerecord (3.2.13) lib/active_record/querying.rb, line 37
ActiveRecord::Relation#exec_queries
activerecord (3.2.13) lib/active_record/relation.rb, line 171
block in ActiveRecord::Relation#to_a
activerecord (3.2.13) lib/active_record/relation.rb, line 160
ActiveRecord::Relation#logging_query_plan
activerecord (3.2.13) lib/active_record/explain.rb, line 34
ActiveRecord::Relation#to_a
activerecord (3.2.13) lib/active_record/relation.rb, line 159
ActiveRecord::Relation#find_first
activerecord (3.2.13) lib/active_record/relation/finder_methods.rb, line 380
ActiveRecord::Relation#first
activerecord (3.2.13) lib/active_record/relation/finder_methods.rb, line 122
ActiveRecord::Associations::HasOneAssociation#find_target
activerecord (3.2.13) lib/active_record/associations/singular_association.rb, line 42
ActiveRecord::Associations::HasOneAssociation#load_target
activerecord (3.2.13) lib/active_record/associations/association.rb, line 145
ActiveRecord::Associations::HasOneAssociation#reload
activerecord (3.2.13) lib/active_record/associations/association.rb, line 55
ActiveRecord::Associations::HasOneAssociation#reader
activerecord (3.2.13) lib/active_record/associations/singular_association.rb, line 9
block in LineItem#product
activerecord (3.2.13) lib/active_record/associations/builder/association.rb, line 44
LineItem#item_price
app/models/line_item.rb, line 10
LineItemsController#create
app/controllers/line_items_controller.rb, line 7

任何见解表示赞赏

4

1 回答 1

0

你跑了rake db:migrate吗?

编辑:: 好的,看看堆栈跟踪,我们有line_items_controller.rb, line 7 @line_item.item_price,这反过来又调用line_item.rb, line 10,这是if size.price.nil? || size.price == 0

所以错误在于调用size. Size.find_by_id(4)在控制台中给你什么?

编辑:: 另外我认为您的关联不正确-我认为sizes表格不应该有line_item_id列,而是line_items表格应该有size_id列。在这种情况下,您想要belongs_to :size.

于 2013-06-25T02:08:11.677 回答