separate use of factory_girl and factory_girl_rails?

120 views
Skip to first unread message

Martin Streicher

unread,
May 17, 2012, 9:26:23 AM5/17/12
to factor...@googlegroups.com
I have an application that uses Redis::Objects in some models (no AR) and ActiveRecord::Base for traditional Rails models. Is there a way for me to use FactoryGirl for the former yet FactoryGirlRails for the latter? 


Joe Ferris

unread,
May 17, 2012, 9:28:24 AM5/17/12
to factor...@googlegroups.com
Hey Martin,

The factory_girl_rails gem doesn't change the way your factories or models work. It just hooks into Rails to provide some nice things, like:

* Autoloading factories after the environment is initialized
* Replacing fixtures with factories
* Providing generators

That means you can use factory_girl_rails with Redis::Objects or other ORMs just as well as factory_girl.

-Joe

On Thursday, May 17, 2012 at 9:26 AM, Martin Streicher wrote:

I have an application that uses Redis::Objects in some models (no AR) and ActiveRecord::Base for traditional Rails models. Is there a way for me to use FactoryGirl for the former yet FactoryGirlRails for the latter? 


--
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

Martin Streicher

unread,
May 17, 2012, 10:00:25 AM5/17/12
to factor...@googlegroups.com
Hello, Joe. Thanks for the prompt response. I do see this...

  1) GamesController show do
     Failure/Error: g = create :game
     NoMethodError:
       undefined method `save!' for #<Game:0x007fe0562f6438>
     # ./spec/controllers/games_controller_spec.rb:6:in `block (3 levels) in <top (required)>'

Game is not an ActiveRecord::Base model. 

Joe Ferris

unread,
May 17, 2012, 10:07:56 AM5/17/12
to factor...@googlegroups.com
Hey Martin,

By default, factory_girl assumes you want to call save! on your model to create it. You can overrider that using to_create:

    FactoryGirl.define do
      factory :game do
        # attributes
        to_create { |instance| instance.commit }
      end
    end

-Joe

Martin Streicher

unread,
May 17, 2012, 10:19:18 AM5/17/12
to factor...@googlegroups.com
Thanks. And I assume here .commit is whatever method name saves the model, not necessarily something required for Redis? 

Joe Ferris

unread,
May 17, 2012, 10:20:37 AM5/17/12
to factor...@googlegroups.com
Hey Martin,

Yes, sorry - I don't actually know how you save things in that particular ORM, so you'd substitute your own method for "commit" there.

-Joe

Martin Streicher

unread,
May 17, 2012, 11:48:03 AM5/17/12
to factor...@googlegroups.com
Got it. Redis is in-memory, so on_create would be a NOP. 



On May 17, 2012, at 10:20 AM, Joe Ferris wrote:

Hey Martin,

Yes, sorry - I don't actually know how you save things in that particular ORM, so you'd substitute your own method for "commit" there.

-Joe

Joe Ferris

unread,
May 17, 2012, 11:48:36 AM5/17/12
to factor...@googlegroups.com
If there's no extra save step, you can also just use build instead of create.
Reply all
Reply to author
Forward
0 new messages