GlyphGryph
GlyphGryph

Reputation: 4794

Why is factory bot nulling out passed in attributes? How can I get it to not do that?

I have a simple factory

  factory :imported_object do
    sequence(:source_id) { |n| "id_#{n}" }
    source { 'default_source' }
  end

and I'm trying to test that it allows duplicate source_ids, but only from different sources. However, I'm not able to assign these attributes at the same time. I can assign either individually, but

imported_object = build(:imported_object, source: '5', source_id: 'different_source')

causes both objects to have the value nil in the resulting imported_object

How can I get FactoryBot to work here?

Upvotes: 1

Views: 719

Answers (1)

Dodecadaniel
Dodecadaniel

Reputation: 131

This has to do with the fact that your columns are called source and source_id. factory_bot makes an assumption that source is an association and source_id is the foreign key for that association.

This issue comes up a lot, and we are tracking it at https://github.com/thoughtbot/factory_bot/issues/1142.

Upvotes: 1

Related Questions