How to define associated associations?

123 views
Skip to first unread message

Thomas Watson

unread,
Aug 28, 2009, 10:35:46 AM8/28/09
to factory_girl
I have a User, a Company and a Department model:
- A user belongs to a department and a company
- A department belongs to a company
- The department that is associated with the user of cause needs to be
associated with the company that's also associated with the user

How do I specify this in factory_girl?

If I just define the following, the department that is created will
not be associated correctly with the company.

Factory.define :user do |f|
f.association :company
f.association :department
end

I could of cause just remove the company association from the user all
together and go through the department to get this. But for database
performance reasons I would like to also have this on the user.

Ryan Angilly

unread,
Aug 28, 2009, 10:41:55 AM8/28/09
to factor...@googlegroups.com
Didnt test this, but I believe it's:


Factory.define :user do |f|
 f.association :company
 f.association :department, :company => f.company
end

Order matters.  Gotta define company before you can use f.company in association.

Thomas Watson

unread,
Aug 28, 2009, 11:23:43 AM8/28/09
to factory_girl
On Aug 28, 4:41 pm, Ryan Angilly <angi...@gmail.com> wrote:
> Didnt test this, but I believe it's:
>
> Factory.define :user do |f|
>  f.association :company
>  f.association :department, :company => f.company
> end

Hmm... If I do this, I get the following error when trying to use the
factory:

.../lib/factory_girl/factory.rb:111:in `add_attribute': Attribute
already defined: company (Factory::AttributeDefinitionError) from
.../lib/factory_girl/factory.rb:132:in `method_missing' from .../spec/
factories/user.rb:18 from
.../lib/factory_girl/factory.rb:46:in `define' from .../spec/factories/
user.rb:16

Mark Wilden

unread,
Aug 28, 2009, 5:32:26 PM8/28/09
to factor...@googlegroups.com
On Fri, Aug 28, 2009 at 7:35 AM, Thomas Watson<w...@tson.dk> wrote:
>
> I could of cause just remove the company association from the user all
> together and go through the department to get this. But for database
> performance reasons I would like to also have this on the user.

Almost OT advice: don't worry about database performance until you
need to. And most of us should be so lucky.

///ark

Thomas Watson

unread,
Aug 31, 2009, 9:00:24 AM8/31/09
to factory_girl
On Aug 28, 11:32 pm, Mark Wilden <m...@mwilden.com> wrote:
> Almost OT advice: don't worry about database performance until you
> need to. And most of us should be so lucky.

Well, I would normally agree with you, but in this case the two
foreign keys are there because of historical reasons and changing it
now would be a headache.

Btw: This would be a nice feature for other reasons as well. One could
imagine that you would like to set a normal attribute on an
association (i.e. It doesn't have to be an associated association like
in my case):

Factory.define :user do |f|
f.association :company, :name => "Foobar Inc."
end
Reply all
Reply to author
Forward
0 new messages