ActiveModel :: MissingAttributeError:无法使用FactoryGirl写入未知属性"ad_id" [英] ActiveModel::MissingAttributeError: can't write unknown attribute `ad_id' with FactoryGirl
问题描述
我有以下型号:
class Ad < ActiveRecord::Base
belongs_to :page
has_one :image
has_one :logo
end
class Page < ActiveRecord::Base
has_many :logos
has_many :images
has_many :ads
end
class Image < ActiveRecord::Base
belongs_to :page
has_many :ads
end
我已经定义了以下工厂:
And I have defined the following Factories:
factory :page do
url 'test.com'
end
factory :image do
width 200
height 200
page
end
factory :ad do
background 'rgb(255,0,0)'
page
image
end
当我尝试这样做时:
ad = FactoryGirl.create(:ad)我收到以下错误 ActiveModel :: MissingAttributeError:无法在我确定广告中图像关联的行中写入未知属性ad_id'
工厂.
ad = FactoryGirl.create(:ad) I get the following error ActiveModel::MissingAttributeError: can't write unknown attribute ad_id'
right in the line where I decide the image association in the ad Factory.
我在这里做什么错了?
推荐答案
当您说:
has_one :image
Rails希望您在 images
表中定义一个 ad_id
字段.考虑到您的关联的组织方式,我假设您有一个 image_id
和一个 logo_id
和一个 ads
表,所以不是:
Rails expects you to define an ad_id
field at the images
table. Given the way your associations are organised, I assume you have an image_id
and a logo_id
a the ads
table so instead of:
class Ad < ActiveRecord::Base
belongs_to :page
has_one :image
has_one :logo
end
您可能是说:
class Ad < ActiveRecord::Base
belongs_to :page
belongs_to :image
belongs_to :logo
end
如果不是这种情况,则需要在 Image
和 Logo
中都添加 ad_id
列.
If that's not the case then you need to add ad_id
columns to both Image
and Logo
.
这篇关于ActiveModel :: MissingAttributeError:无法使用FactoryGirl写入未知属性"ad_id"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!