2
我正在尝试为我正在开发的Rails应用程序构建基本购物车。has_one关联和工厂女孩的任意属性错误
没什么特别的,
- 购物车中有许多line_items
- 相关联的每个LINE_ITEM HAS_ONE产品与它
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
是的,我认为就是这样!我多么愚蠢。这可能是一个愚蠢的问题,但为了帮助我理解 - 因为我通常只在视图和控制器层中工作,所以模型对我来说还是比较新的 - 为什么在产品模型中需要对应模型?对于协会来说,情况总是如此,或者在某种情况下它是单向的吗? – purpletonic
......嗯,事实并非如此。因为我仍然收到通知: '退化警告:您正尝试创建属性'line_item_id'。在模型上编写任意属性已被弃用。请使用'attr_writer'等(在中的块(4级)调用/code/spec/factories.rb:104)' –
purpletonic
此文件中的行104有什么代码?请粘贴相关的代码,并附上一些注释,指示104行。 – Salil