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
How Factory Girl create association with existing object? #683
Comments
I have the same issue or question. Is there any way to prevent this from happening? |
+1, same question as this makes difficult for me to test some things and this is not documented |
Ran into a similar problem where we had two associations attempting to create unique parent instances when we wanted them to belong to the same parent. Class Term < ActiveRecord::Base
has_many :candidates
has_many :periods
end
Class Period < ActiveRecord::Base
belongs_to :term
end
Class Candidate < ActiveRecord::Base
belongs_to :term
end In order to avoid needing to make multiple factory calls from the spec to reach the same term we ended up defining the factories like this: factory :period do
term { Term.first || association(:term) }
end
factory :candidate do
term { Term.first || association(:term) }
end This way the pre-existing Term can be referred to instead of creating two instances. |
Calling |
@joshuaclayton is there any chance that you could give some practical example to how to do that? |
@scaryguy sure thing! term = create(:term)
period = create(:period, term: term)
candidate = create(:candidate, term: term) |
Thank you @joshuaclayton ! |
From OP: class Girl < ActiveRecord::Base
belongs_to :boy
end
class Boy < ActiveRecord::Base
has_one :girl
end For real? Girl belongs to boy didn't feel a little gross to anyone? Let's write examples without reducing women to objects, please, and call this stuff out when we see it. |
@Euraldius, both |
Bumping this, OPs question still isn't answered. In the original post, OP is already using the suggestion provided here. The only solution I can come up with is to say (in the context of OP example)
But this doesnt seem very idiomatic. Also, at least where I live, there is a a constant, subtle, barrage of messaging coming from the TV and other sources telling us that women are the property of men. This is insidious, and FWIW, I think we should try to be conscious not to take part. cc @linkyndy. |
just use so you could have something like
Then do something like
|
@linkyndy Do you seriously think @Euraldius did not understand that those labels referred to Ruby objects? Let me slowly explain to you the point that was being made: When engineers use labels such as "boy"/"girl" in ways that reinforce toxic gender stereotypes, such engineers show their ignorance and (perhaps unconscious, perhaps latent) misogyny. So, stop. Think about how you use your language. And be considerate. Another related example: the widespread use of repulsive "master"/"slave" labels in software systems. Just fucking think about it for a second... bro. |
Bad example or not, the issue has been handled on this and many other levels. We still haven't really found a great solution to the original problem, but if the only submissions here are going to be in regards to the social issues involved, then the should be locked. @Euraldius @joshuaclayton |
I solved this problem by using
And then @Manifold0 I used an after-create callback on the factory for the model without the foreign key, so they look like this:
In the callback, @Kriechi this will help your test performance by not creating extra throwaway records, and also affect your actual production performance by making fewer trips to the db. Which is the actual purpose of |
What do you think about: factory :singleton do
initialize_with do
Singleton.where(
name: 'unique_name'
).first_or_initialize
end
end this moves the logic to the definition of your singleton factory. |
If you're really working with a Singleton, I'm not sure you need FactoryBot at all. If it's not really a singleton, than first_or_initialize has the non-determinism problem mentioned above. Am I missing something about what you're trying to do? |
I feel hurt by the word Singleton because it reminds me of who I think I am. This message is not trollish intent to hurt anyone, rather a part of a philosophical debate. (cc @skatenerd) I wholeheartedly agree that we should fight any discrimination. |
@mib32 sry i got a bit defensive, seeing how the original namechange discussion ... turned out |
@drewcimino The requirement in my case was that there is only one entry with a specific name in the db table. Every reference to a |
i can see how the girl-belongs-to-boy rubs the wrong way, linkyndy. imagine a similar example, only swap the Girl model to Black and Boy model to White. |
@adler99 So it seems your Singleton class isn't really a singleton, but does have relatively few saved records, like some sort of reference data. If the objects of this class are that standardized - that is, a record with a given name always has the same associated data - I would create them at the beginning of the test suite with a seeds-file-like script, and building some class methods for accessing them. Something like:
as opposed to:
|
Using the
Is it possible to create the Probably a wrong example here but is it possible to do it inside (sometimes an object belongs to an object that belongs to an object, and if we were to manually create each object from the top and reference the next one outside of the factorybot file then it would be a mess everywhere), something like this:
|
@CyberMew If you want to complete the period and candidate with Bear#term after Bear/Term are created, I think an
I use You can force build strategies if you want to, but I don't recommend it as you can easily end up with some funky saved/not-saved "associated" records. But there's a bit on that here: https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#associations |
I came here for code and got distracted by social issues. I don't have a problem with discussing our society. In fact, I'd like to know, who decides which social issues are worth discussing while working? Is it as simple as me labeling my issue an injustice - then I proceed to make my case? Currently, I have access to this comment box, so I suppose I have an equal right to say - but can someone point me to the rules? I opted for
|
I believe we need |
Came here because of a frustrating engineering problem, disappointed to see people are discussing irrelevant social issues.
|
As a reminder, anybody participating here agrees to follow our Code of Conduct. A code example where a girl belongs to a boy does not follow our Code of Conduct and I appreciate the folks who called that out. I'm going to lock this issue. If somebody would be willing to open a new issue with a better example and a summary of the problem and potential solutions offered so far I would be most grateful. |
I have two associated models.
And I define factory girl for them:
Then i create a girl with an exsisting boy:
I will get following result:
I am wondering why the block of name of boy runs twice.
And, if i put code that can only runs once, troubles come.
Version of my factory_girl:
The text was updated successfully, but these errors were encountered: