当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby on rails - Building a record with polymorphic assocation with FactoryGirl

问题描述:

I would like to know why I am getting an error when I am building a record using FactoryGirl.

Comment model belongs to user and commentable through polymorphic association.

My factory looks like this:

FactoryGirl.define do

factory :comment do

text "Some text"

user_id 1

association :commentable, factory: :feedback

end

end

When I'm build a comment record using FactoryGirl, I get:

ActiveModel::MissingAttributeError:

can't write unknown attribute `commentable_type'

I have no problem creating and manipulating Comment object in console. I can set and save commentable_type attribute freely. Also, commentable_type already exists in the comments table.

Any thoughts on why the error occurs?

网友答案:

I ran all my migrations again using rake db:reset and now the error has disappeared. Before that, I have tried to rollback the related migration and migrate again, but that did not make any change.

I still do not know what caused this error, but for now, it is fixed.

分享给朋友:
您可能感兴趣的文章:
随机阅读: