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 10if size.price.nil? || size.price == 0

したがって、エラーは呼び出しにありsizeます。コンソールで何がSize.find_by_id(4)得られますか?

編集::また、あなたの関連付けが間違っていると思います-テーブルには列が必要ではなく、テーブルに列sizesが必要だと思います。その場合、必要です。line_item_idline_itemssize_idbelongs_to :size

于 2013-06-25T02:08:11.677 に答える