Association infinite loop

75 views
Skip to first unread message

Bill Kocik

unread,
Jul 2, 2009, 11:57:08 AM7/2/09
to factory_girl

I have these relationships in my models:

class User < ActiveRecord::Base
has_many :groups, :include => [:group_memberships], :dependent
=> :destroy
belongs_to :default_group, :class_name => 'Group'
....
end

class Group < ActiveRecord::Base
belongs_to :user
...
end

My group test needs to ensure that when a test group is created, it
has a parent user. Likewise, when a test user is created, it must have
a set of groups, one of which is the default. So I have these in my
factories.rb:

Factory.define :user, :default_strategy => :build do |f|
...
f.groups {|groups| [groups.association(:group)]}
f.default_group {|d| d.groups.first}
end

Factory.define :group, :default_strategy => :build do |f|
f.sequence(:name) {|n| "Test Group #{n}"}
f.association :user
end

The problem is, if I turn off either the f.association :user line in
my group factory, or the f.groups line in my user factory, then my
tests for that model fail because the association isn't put together.
But with both of those enabled, then my tests for either model go into
infinite loops.

What am I doing wrong?

Mike

unread,
Jul 21, 2009, 7:35:32 AM7/21/09
to factory_girl
I have the same problem here and dont know how to solve this in a nice
way.
A workaround is creating the association on one side by hand in the
tests.

Ryan Angilly

unread,
Jul 21, 2009, 9:21:14 AM7/21/09
to factor...@googlegroups.com
Hey guys,

This is untested, but I think you can do something like this:


Factory.define :user, :default_strategy => :build do |f|
 ...
 f.groups {|groups| [groups.association(:group)]}
 f.default_group {|d| d.groups.first}
end

Factory.define :group, :default_strategy => :build do |f|
 f.sequence(:name) {|n| "Test Group #{n}"}
 f.user nil
end

and then:

@user = Factory :user
@group = Factory :group, :user => @user

mike

unread,
Jul 21, 2009, 9:58:35 AM7/21/09
to factor...@googlegroups.com
Hi Ryan,
that is what i wanted to say :) thanks for your message though
Mike

Ryan Angilly

unread,
Jul 21, 2009, 10:08:10 AM7/21/09
to factor...@googlegroups.com
Yeah I dont think there's anything not nice about doing it that way.  I think it's just 'the way'.  Unless Mr. Ferris chimes in.. then whatever he says is 'the way'  :)

Cheers,
Ry

Bill Kocik

unread,
Jul 22, 2009, 11:52:45 AM7/22/09
to factor...@googlegroups.com
On Tue, Jul 21, 2009 at 9:21 AM, Ryan Angilly<ang...@gmail.com> wrote:
> Hey guys,
>
> This is untested, but I think you can do something like this:

It's tested now, and it worked beautifully. So brilliant it's simple. :)

Thanks!

> Factory.define :user, :default_strategy => :build do |f|
>  ...
>  f.groups {|groups| [groups.association(:group)]}
>  f.default_group {|d| d.groups.first}
> end
>
> Factory.define :group, :default_strategy => :build do |f|
>  f.sequence(:name) {|n| "Test Group #{n}"}
>  f.user nil
> end
>
> and then:
>
> @user = Factory :user
> @group = Factory :group, :user => @user


--
Bill Kocik

http://bkocik.net

Reply all
Reply to author
Forward
0 new messages