how to force an associated object when seeding data

143 views
Skip to first unread message

kadoudal

unread,
May 8, 2012, 12:01:58 PM5/8/12
to factory_girl
I am trying to seed data in my test DB using factory_girls

I have some static data created like :
puts "CREATE AREAS"
west = Area.create(name: "West)
north = Area.create(name: "North)
east = Area.create(name: "East)
south = Area.create(name: "South)
..
and I wrote
FactoryGirl.define do
factory :partner do
area
......
phone {....}
address { ...}


I want to seed partners for these areas, forcing the associated area
( west, north, east , south )
FactoryGirl.create(:partner) ??
how should I wrote it ?

thanks for feedback

Chris Bloom

unread,
May 9, 2012, 12:12:56 AM5/9/12
to factor...@googlegroups.com
On Tuesday, May 8, 2012 12:01:58 PM UTC-4, kadoudal wrote:
I have some static data created like :
puts "CREATE AREAS"
west = Area.create(name: "West)
north = Area.create(name: "North)
east = Area.create(name: "East)
south = Area.create(name: "South)
..
and I wrote
FactoryGirl.define do
  factory :partner do
    area
   ......
    phone {....}
    address { ...}

FactoryGirl.create(:partner, area: west) 
FactoryGirl.create(:partner, area: north) 
FactoryGirl.create(:partner, area: east) 
FactoryGirl.create(:partner, area: south) 

kadoudal

unread,
May 9, 2012, 2:03:37 AM5/9/12
to factory_girl
Thanks Chris

That's what I tested first, but I get an error :

FactoryGirl.build(:private_partner, area: ouest)

$ rake RAILS_ENV=test db:seed
rake aborted!
Trait not registered: area

as I have in my factories
factory :partner do
area
...
trait :private do
legal_framework {...}
...
end
...
trait :company
legal_framework {...}
..
end
factory :private_partner, traits: [:private]
factory :company_partner, traits: [:company]
end

I may have to indicate area inside each trait ?

kadoudal

unread,
May 9, 2012, 2:40:03 AM5/9/12
to factory_girl
after re-tseting , it seems that the association SHOULD be written
inside the factory redefinitions ... is that right ? ( not specified
in the doc ..)

factory :partner do
...
trait :private do
area
legal_framework {...}
...
end
...
trait :company
area

Joe Ferris

unread,
May 9, 2012, 9:29:36 AM5/9/12
to factor...@googlegroups.com
Hey,

Do you have an area factory? If you define an attribute without a value, factory_girl expects a factory, sequence, or trait to be defined by that name. Otherwise, it doesn't know how to supply a value for that attribute.

-Joe
--
Individuals over processes. Interactions over tools. Agile Rails training from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:

You received this message because you are subscribed to the "factory_girl" mailing list.
To post to this group, send email to factor...@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group at

kadoudal

unread,
May 9, 2012, 12:51:35 PM5/9/12
to factory_girl
Thanks a lot ! Got it...
> > To post to this group, send email to factor...@googlegroups.com (mailto:factor...@googlegroups.com)
> > To unsubscribe from this group, send email to
> > factory_girl...@googlegroups.com (mailto:factory_girl...@googlegroups.com)
Reply all
Reply to author
Forward
0 new messages