Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workaround for attributes with _id #1708

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions docs/src/cookbook/attributes-with-id-suffix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Attributes with _id suffix

When using FactoryBot in a rails application, defining factory attributes ending with `_id` may cause an unexpected behaviour:

```ruby
# Migration:
create_table :realtors, force: true do |t|
t.boolean :duplicate, default: false, null: false
t.string :duplicate_id
end

# Factory
FactoryBot.define do
factory :realtor do
duplicate { false }
duplicate_id { "This should exist" }
end
end
```

```
realtor1 = create(:realtor, id: 6, duplicate: true, duplicate_id: 'dup1')
[1] pry> realtor1.duplicate_id
=> nil
```

A fix for this would be to make sure the special behavior related to `_id` is exclusive to `factory_bot_rails`, since `factory_bot` is framework-agnostic.

A workaround can be to use trasients to declare those attributes instead of a regular attribute declaration

```
transient do
duplicate { false }
duplicate_id { "This should exist" }
end

initialize_with do
new(attributes.merge(duplicate:, duplicate_id:))
end
```

You can also override the list of aliases to include the attributes you want to exclude:

```
FactoryBot.aliases = [
[/([^(?:duplicate)].+)_id/, '\1'],
[/([^(?:duplicate)].*)/, '\1_id']
]
```

Loading