FactoryGirl association_id是零

时间:2018-04-29 13:21:00

标签: ruby-on-rails rspec factory-bot

我正在尝试将factory_girl与关联一起使用。我有2个模型:account_info和person。 AccountInfo属于我的account_info工厂的人员:

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

它创建记录Person但AccountInfo entity_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"> 

版本:

factory_girl_rails (4.9.0)
rails (5.1.6)
ruby (2.5.0)

1 个答案:

答案 0 :(得分:2)

奇怪的是annotation.interiorColor = [NSColor redColor];未定义。您是否在AccountInfo模型中定义了关联? (例如entity=)?如果是这样,那可能是问题(或类似的东西)。