Ruby on rails FactoryGirl association\u id为零
我正试图利用工厂女孩与协会。我有两个模型:帐户信息和个人。AccountInfo属于“我的帐户”\u信息工厂中的某个人,如下所示:Ruby on rails FactoryGirl association\u id为零,ruby-on-rails,rspec,factory-bot,Ruby On Rails,Rspec,Factory Bot,我正试图利用工厂女孩与协会。我有两个模型:帐户信息和个人。AccountInfo属于“我的帐户”\u信息工厂中的某个人,如下所示: FactoryGirl.define do factory :account_info do entity_type 1 account_key { Faker::Twitter.user[:id] } association :entity_id, factory: [:person] association :social_m
FactoryGirl.define do
factory :account_info do
entity_type 1
account_key { Faker::Twitter.user[:id] }
association :entity_id, factory: [:person]
association :social_media_site_id, factory: [:twitter]
end
end
根据文件:
You can also specify a different factory or override attributes:
factory :post do
# ...
association :author, factory: :user, last_name: "Writely"
end
The behavior of the association method varies depending on the build strategy used for the parent object.
# Builds and saves a User and a Post
post = create(:post)
post.new_record? # => false
post.author.new_record? # => false
# Builds and saves a User, and then builds but does not save a Post
post = build(:post)
post.new_record? # => true
post.author.new_record? # => false
它创建记录人,但AccountInfo实体id保持为零。为什么?
d = FactoryGirl.create(:account_info)
d.reload
AccountInfo Load (0.5ms) SELECT "account_infos".* FROM "account_infos" WHERE "account_infos"."id" = $1 LIMIT $2 [["id", 443], ["LIMIT", 1]]
=> #<AccountInfo id: 443, entity_type: "legislator", account_key: "498133264559673104", social_media_site_id: nil, entity_id: nil, created_at: "2018-04-29 13:18:49", updated_at: "2018-04-29 13:18:49">
奇怪的是,
entity=
没有定义。您是否在AccountInfo模型上定义了关联?(例如属于:实体)?如果是这样,那可能就是问题所在(或类似的问题)。奇怪的是,实体=
没有定义。您是否在AccountInfo模型上定义了关联?(例如属于:实体)?如果是这样,那可能是问题(或类似的问题)。关联应该是实体
而不是实体id
NoMethodError(未定义的方法`entity='for#你的意思是什么?实体id=)如果我这样做,你的AccountInfo
模型上定义了关联吗?(例如属于:实体)?是的,但我认为这无关紧要。然而你的回答让我思考,你是完全正确的。这是其中的一句话。请发布这个答案,足够了:D@TaryEastAssociation应该是entity
而不是entity\u id
NoMethodError(你的意思是什么?entity\u id=)如果我这样做了,你的AccountInfo
模型上定义了关联吗?(例如属于:实体)?是的,但我认为这无关紧要。然而你的回答让我思考,你是完全正确的。这是其中的一句话。请发布这个答案,足够了:D@TarynEast
factory_girl_rails (4.9.0)
rails (5.1.6)
ruby (2.5.0)