0
我有以下设置:Rails3中FactoryGirl HAS_ONE未知属性错误
型号:
class Product < ActiveRecord::Base
has_one :product_category
attr_accessible :name, :product_category, :product_category_id
end
class ProductCategory < ActiveRecord::Base
belongs_to :product
attr_accessible :name
end
迁移:
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.references :product_category
t.string :name
t.timestamps
end
end
end
class CreateProductCategories < ActiveRecord::Migration
def change
create_table :product_categories do |t|
t.string :name
t.timestamps
end
end
end
现在,我想测试使用FactoryGirl和RSpec。所以我设置了以下FactoryGirl测试机型:
product_spec.rb
require 'factory_girl'
FactoryGirl.define do
factory :product, class: Product do
product_category {|a| a.association(:product_category)}
name "Demo Product"
end
end
product_category_spec.rb
require 'factory_girl'
FactoryGirl.define do
factory :product_category, class: ProductCategory do
name "Demo Product"
end
end
但是当我在product_spec.rb运行RSpec的,我得到以下错误:
can't write unknown attribute 'product_id'
我不明白为什么会发生这种情况。如果我从产品工厂中删除product_category,则一切正常。
呃,男人,我觉得哑巴已经犯了这么简单的错误。感谢您的快速回复! – Bryce