What if FactoryGirl depends on seeds?

65 views
Skip to first unread message

Bradford Smith

unread,
Feb 4, 2013, 10:22:31 AM2/4/13
to factor...@googlegroups.com
I have State and Country that my app needs to run.   I seed it in my test, development, and production environments.  I use FactoryGirl to create a store:

FactoryGirl.define do
  factory :store do
    name "Best Store Ever"
    city "Somewhere"
    state_id State.find_by_abbreviation("CA").id
    country_id Country.find_by_iso("US").id
  end
end

But, FactoryGirl seems to get run before my seed and I get an error:  Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id (RuntimeError)

State.abbreviation and Country.iso are unique.

What are my best options for doing this?  Should FactoryGirl not rely on seed data?  How else should I do this?  I have some ideas, but would like to know what the best practice is here.

Rennan Maciel

unread,
Feb 4, 2013, 6:27:15 PM2/4/13
to factor...@googlegroups.com
I've never used seeds with factory girl, but I believe having it as:


FactoryGirl.define do
  factory :store do
    name "Best Store Ever"
    city "Somewhere"
    state_id { State.find_by_abbreviation("CA").id }
    country_id { Country.find_by_iso("US").id }
  end
end

should do the trick, as whatever is between {} (passed as a block) will only be executed when called

--
--
Individuals over processes. Interactions over tools. Agile Rails training from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:
http://thoughtbot.com/services/training
 
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
factory_girl...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/factory_girl?hl=en
---
You received this message because you are subscribed to the Google Groups "factory_girl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to factory_girl...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Rennan Maciel de Oliveira
Desenvolvedor Web na Superintendência de Tecnologia da Informação (STI)
Universidade Federal Fluminense (UFF)
Escritor wannabe nas horas vagas
+55 (21) 9248-8485

Bradford Smith

unread,
Feb 4, 2013, 9:48:22 PM2/4/13
to factor...@googlegroups.com
Thanks, Rennan.  I realized that right after I posted this, but the post took some time to go through. 

Thanks again,
Bradford
Reply all
Reply to author
Forward
0 new messages