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

Dependent Associations #426

Closed
Ajedi32 opened this issue Aug 15, 2012 · 11 comments

Comments

@Ajedi32
Copy link

commented Aug 15, 2012

FactoryGirl's support for dependent attributes needs to be extended to work with associations. Currently, you have to use ugly workarounds like this: Stack Overflow: Get two associations within a Factory to share another association

@mikelewis

This comment has been minimized.

Copy link

commented Aug 18, 2012

+1

@joshuaclayton

This comment has been minimized.

Copy link
Member

commented Aug 20, 2012

What were you thinking for a syntax?

On Aug 18, 2012, at 2:22, mikelewis notifications@github.com wrote:

+1


Reply to this email directly or view it on GitHub.

@Ajedi32

This comment has been minimized.

Copy link
Author

commented Aug 21, 2012

Is there any reason it can't be exactly the same as the syntax for regular dependent attributes?

factory :foo do
  bar_relationship
  dependent_relationship { FactoryGirl.build(:dependent_relationship, bar_relationship: bar_relationship) }
end

Although, perhaps a cleaner way would be to use the syntax suggested in the StackOverflow post I referenced:

FactoryGirl.define do

  factory :relationship do
    association :guardian
    association :student, :school => self.guardian.school
    relationship_type RelationshipType.first
  end

end
@joshuaclayton

This comment has been minimized.

Copy link
Member

commented Aug 21, 2012

The first syntax will work perfectly fine; in fact, if you're using FactoryGirl 3.2.0+, you can just do:

factory :foo do
  bar_relationship
  dependent_relationship { build(:dependent_relationship, bar_relationship: bar_relationship) }
end

The latter syntax would be very difficult to do due to the dynamic nature of calculating values from the association, but the former works fine.

Good luck!

@hmayer00

This comment has been minimized.

Copy link
Contributor

commented Apr 24, 2013

Thanks Josh, that works well for me. One question: this specifies the "build" strategy for the dependent relationship - is there a way to do this in a way that defers to whatever the main factory's strategy is?

Thanks!

@joshuaclayton

This comment has been minimized.

Copy link
Member

commented Apr 24, 2013

@hmayer-rp there's not a good way to do that sadly. It's one of the last big problems that I've had trouble with figuring out how to do (not so much from a code standpoint but from what makes sense). The discussion was started here #458 with a link to a branch I started a while ago (https://github.com/thoughtbot/factory_girl/tree/produce-association) that has some ideas. I haven't rebased against master but if you're interested in trying this out locally, I could get that branch up-to-date and you could try it locally to see how it feels and if you run into any bugs...

Let me know and I should be able to tackle this on Friday!

@hmayer00

This comment has been minimized.

Copy link
Contributor

commented Apr 24, 2013

Wow, thanks for the super fast response!

I'd love to try this out. We actually are pretty terrible about our factory usage in some ways - pretty much everything is a create, which as you might expect makes our tests a lot slower (though also easier to write - have definitely had the occasional gotcha related to not doing the full create). So being able to make dependent relationships strategy-agnostic would help us move in that direction.

However i have to admit that i'm not likely to be any use to you for the next couple of weeks (deadlines, etc.) So I wouldn't want you to rush to get that branch up to date only to have it languish without any use. We're planning to take on some technical debt in the next month or so, and i'd like to spend a bit of that time auditing and tweaking our factory usage - but i probably won't really get to this until then.

Can I ping you when we're moving that direction and see if you still have time?

And thanks again for the excellent gem and the quick turnaround.

@joshuaclayton

This comment has been minimized.

Copy link
Member

commented Apr 24, 2013

@hmayer-rp that sounds perfect; I've been putting it off (as you can see from the fact that I haven't touched it in five months or so) but definitely ping me on this thread when you're ready. I'll refactor a bit and get it up-to-date against master before that point regardless and try it out on some of my own stuff too.

@Systho

This comment has been minimized.

Copy link

commented Nov 27, 2013

Any news on this feature ? It would really be awesome to be able to declare associations with dependent attributes and respect initial strategy

@joshuaclayton

This comment has been minimized.

Copy link
Member

commented Dec 1, 2013

@Systho nothing of note yet, but I'll be sure to tie the commit(s) to the appropriate PRs and issues once there's progress made!

@CodingAnarchy

This comment has been minimized.

Copy link

commented Nov 4, 2016

I'm curious if there's any intent to work on this (I know it's pretty old) - I've got some slow tests that would be sped up drastically by being able to defers to whatever the main factory's strategy instead of use create every time because some tests will require the full create.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.