Faker, Ruby on Rails Snippet & Generator

Written on 7:07:00 PM by S. Potter

Hey blog readers (if there are any left), I know it has been a while, and I know I have left you high and dry, so today I decided I will share some code and software development thoughts with you. I am moving to a more snippet oriented blog posting style from now on. Previously I preferred long ramblings about the political ideology of programming languages and even how Ann Coulter was _more_ comprehensible when I used my Metafusion Crypto library on her words (actually that was a marketing lie - she sounded just as incomprehensible using Metafusion Crypto as she did when not using it). Of course, this was all in 2006-2007 when blogging was all about spewing small tidbits that taught the reader nothing, so I had to be different and attempt to find a reason for things. Now things are swinging so I am trying to do the opposite from the norm again. Ok, on with it. Over the last week I have been using the Rails 2.3.4 "seed" mechanism, which in essence attempts to provide 80% of seed-fu's features inside Rails core. In addition on my personal projects I have been using machinist as my factory framework of choice (although I am forced to use factory_girl for my clients), through which I was introduced to faker. A beautiful gem indeed. So to cut a long story short, I have found myself doing the following a hell of a lot in my brand new Rails 2.3.4 projects this week: # in db/seeds.rb def load_seed_files! seed_files = Dir["#{RAILS_ROOT}/db/seeds/all/*.rb"] + Dir["#{RAILS_ROOT}/db/seeds/#{RAILS_ENV}/*.rb"] seed_files.each do |file| puts "Loading seed file: #{file}" load(file) end end load_seed_files! # When I add files to the db/seeds/all directory it seed ALL environments # When I add files to the db/seeds/{development,staging,test,production} directories it seeds # for only that environment. Note: I wouldn't recommend using seeds in just staging, test or production generally. If you need seed data in these environments you _probably_ need them in all environments OR (in the case of the test environment) you need to use a factory framework like machinist or factory_girl. Now where is faker? Well it's what I use inside these .rb files where faker comes into play: # Faking a company name >> Faker::Company.name => "King-Ruecker" >> Faker::Company.name => "Cartwright, Prosacco and Smith" >> Faker::Company.name => "Pagac, Adams and Barrows" >> Faker::Company.name => "Satterfield and Sons" >> Faker::Company.name => "Blick-Deckow" # Faking a person's name >> Faker::Name.first_name => "Ruthe" >> Faker::Name.first_name => "Jameson" >> Faker::Name.first_name => "Avery" >> Faker::Name.first_name => "Jordan" >> Faker::Name.first_name => "Ruthe" >> Faker::Name.first_name => "Winona" >> Faker::Name.last_name => "Brown" >> Faker::Name.last_name => "Hayes" >> Faker::Name.last_name => "Jerde" >> Faker::Name.last_name => "Lindgren" >> Faker::Name.last_name => "Considine" >> Faker::Name.last_name => "Rodriguez" >> Faker::Name.last_name => "Turner" >> Faker::Name.name => "Mekhi Lebsack" >> Faker::Name.name => "Esteban Brown" >> Faker::Name.name => "Velma Crona" >> Faker::Name.name => "Rollin Zemlak" >> Faker::Name.name => "Casper Sanford" # My favorite is coming up with BS (yes, BS is what you think it is): >> Faker::Company.bs => "redefine ubiquitous e-commerce" >> Faker::Company.bs => "synergize viral e-business" >> Faker::Company.bs => "innovate real-time experiences" >> Faker::Company.bs => "aggregate scalable systems" >> Faker::Company.bs => "e-enable back-end functionalities" >> Faker::Company.bs => "implement 24/365 models" So the point of faker, if you haven't already figured out is that it generates data the way you would probably have in production so you are generating meaningful data in your development environment(s) where you want to show your client what the interface will look like with data inside. Now a word of caution: I would not advise you to use this in an end-user facing test environment. I sometimes call that staging or qa, but ultimately it is an environment where real end-users or clients test out the features in a stable environment (not development or continuous integration environment) so they can sign off on your stories. In these environments you should let the user create the data themselves using the admin forms or whatever UI you provide them because you want to see how they will break the system (and we all know they will somehow). It gives you a much better indication of how things will end up working in production, which is very important. Thanks for reading and let me know if this is yet again too rambly? How does it happen? I have the best intentions to just blurt out tidbits with no nuance, yet I always end up rambling on about things and give it a thought process (well sort of). Anyone want to be my editor to strip out all thought and insight so I can be just blog brainless "tip" blog posts like everyone else in the Rails community that wanted to be a micro celebrity (trust me I couldn't handle that kind of paparazzi attention and I don't even have a Macbook Pro to sling over my shoulder in a fashionable messenger bag)? I can't pay you anything, but you could share in my measely $0.63 that Google Adsense pays me each month (actually they don't actually pay until a threshold is met). You could buy one third of an MP3 from iTunes each month. Any takers? What an awesome deal! No really! PS Coming soon a simple generator and Rails application template (or applate as I like to call them) that will set you up with this so you don't have to copy and paste all the time. It'll be on GitHub.com/mbbx6spp. Ta ta. Update: As Damien in the comments pointed out indirectly, I really didn't do Faker justice. Please read the Faker RDocs for more information.

If you enjoyed this post Subscribe to our feed


  1. Nola Stowe |

    I like that bs method hehehe... I seem to remember the module from a few years ago or one like it!.

  2. Damien |

    There's also checkout :
    Which looks more complete.

  3. S. Potter |

    @Damien I would not say it is more complete than Faker. I just didn't do it justice in my post. I only gave a couple of examples.

    Please review RDOC of Faker for more details:

    But I am glad you pointed out the deficiency in my blog post.

  4. Stefano |

    I don't know if it taught me something, but I enjoyed it, thank you !

  5. S. Potter |

    @Stefano: oh no, I can't teach anything. I just ramble!:)

  6. fractious |

    On a slight tangent- only having experience with factory_girl I'd be interested to hear why you favour Machinist.

  7. Mark |

    Per simplifying seeds handling with the new rails seed functionality, you may want to look at:



Post a Comment