Skip to content

Support :traits option on implicit association#1552

Closed
r7kamura wants to merge 1 commit intothoughtbot:mainfrom
r7kamura:feature/implicit-association-traits
Closed

Support :traits option on implicit association#1552
r7kamura wants to merge 1 commit intothoughtbot:mainfrom
r7kamura:feature/implicit-association-traits

Conversation

@r7kamura
Copy link
Copy Markdown
Contributor

@r7kamura r7kamura commented Oct 25, 2022

If we want to specify traits in implicit associations, we have to use :factory option. This is not only a naming problem, but also redundant in implicit style, because when using :factory option, the main factory name must always be specified, so the same name must be written twice.

  user factory: [:user, :trait1, :trait2]
# ^^^^            ^^^^

To resolve this problem, I propose to allow :traits option on implicit associations in this Pull Request.

  user traits: [:trait1, :trait2]

@vburzynski
Copy link
Copy Markdown
Contributor

I noticed this PR had been open for some time and took a look at it. Thanks for taking the time to submit it so long ago.

I liked the idea of not having to add in the redundant factory declaration just to declare some traits. I hesitated, however, at the idea of adding another special option in addition to the existing :factory one, as this would conflict with being able to override attributes named :traits. This all spurred me into digging into why the implicit syntax doesn't operate more like the explicit :association syntax. I think I found an alternate fix:

#1796

With that change, it should be possible to write your example as:

user :trait1, :trait2

quite similar to how the explicit syntax works:

association :user, :trait1, :trait2

...and made possible because static attributes were deprecated in factory_bot 4.11 and removed in 5.x.

As a result, I'm going to close this PR in favor of the other one. Thanks again for the contribution 😎

@vburzynski vburzynski closed this Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants