2

作業中のRailsアプリ用の基本的なショッピングカートを作成しようとしています。

特別なことは何もありません
-ショッピングカートには多くのline_itemsがあります
-各line_itemhas_1つの製品とそれに関連する数量

 class Cart < ActiveRecord::Base
   attr_accessible :line_items
   has_many :line_items, :dependent => :destroy
 end

 class LineItem < ActiveRecord::Base
   attr_accessible :quantity, :product

   belongs_to :cart
   has_one :product
 end

RSpecを使用してこの関連付けをテストしようとしていますが、次のようなエラーが発生するため、何か問題が発生していますDEPRECATION WARNING: You're trying to create an attribute 'line_item_id'. Writing arbitrary attributes on a model is deprecated。理由はわかりません。

私のfactories.rbファイルでは、line_itemファクトリを次のように定義しています。

factory :line_item do
  quantity { Random.rand(1..5) }
  product
end

factory :cart do
  factory :cart_with_two_line_items do
    ignore do
      line_item_count 2
    end

    after(:create) do |cart, evaluator|
      FactoryGirl.create_list(:line_item, evaluator.line_item_count, cart_id: cart) # < 104
    end
  end
end

私が間違っているポインタは、おそらく基本的なものですが、私はまだRspecにまったく慣れていません。前もって感謝します。

編集:line_item_spec.rb

require 'spec_helper'

describe LineItem do
before do
  @line_item = FactoryGirl.create(:line_item)
end
4

1 に答える 1

3

製品モデルで関連付けを宣言するのを忘れた可能性があります。

class Product < Activerecord::Base
  belongs_to :line_item

所属_toは、製品テーブルに列:line_item_idがあることを想定しています。移行を実行してモデルを変更しましたか?

于 2012-06-19T15:12:56.793 に答える