Building has_many association w/o stack overflow

6 views
Skip to first unread message

Pete Deffendol

unread,
Sep 3, 2009, 2:03:57 PM9/3/09
to factory_girl
Hi,

I have two factories. The first's model contains a has_many
relationship to the second's model. I want the first factory to build
an instance of the second when accessed. So I tried this:

# ContactRole has_many :contexts
Factory.define :contact_role do |r|
r.name 'Primary Contact'
r.description 'Test Role'
r.contexts do |contexts|
[contexts.association(:contact_role_context)]
end
end

# ContactRoleContext belongs_to :role
Factory.define :contact_role_context do |c|
c.association :role, :factory => :contact_role
c.add_attribute :context, 'Place'
end

When I execute Factory.build(:contact_role) I get a stack overflow,
presumably because of the circular dependency here. Is there a way to
set this up so that it works?


Ryan Angilly

unread,
Sep 3, 2009, 2:08:19 PM9/3/09
to factor...@googlegroups.com
Hey Pete,

This has been addressed several times on the mailing list.  Go take a look through the mailing list archives, and I think you'll find a bunch of examples.

-Ryan
Reply all
Reply to author
Forward
0 new messages