Blog Post RSS ?

Blogs » Ruby on Rails » Flexible Fixtures in Rails 2
 

Flexible Fixtures in Rails 2


  • Save to
    Del.icio.us

by Myles Eftos

As Matt Magain pointed out yesterday, Rails 2.0 is now gold! Not a lot has changed feature wise from the PR (makes sense - features were frozen at that point), although it seems that the new improvements to fixtures managed to slip in to the final version.

Rather than having to map foreign keys in your fixtures using id numbers, you can use fixture names, which makes life a whole lot easier. So you can now write:

users.yml

joe_blogs:
  id: 1
  first_name: Joe
  last_name: Blogs
mary_smith:
  id: 2
  first_name: Mary
  last_name: Smith


websites.yml

website_1
  id: 1
  user: joe_blogs
  url: "http://www.joeblogs.com"

website_2
  id: 2
  user: mary_smith
  url: "http://mary.smith.id.au"

which obviously makes a lot more sense to a human reading it, especially when you have a large number of fixtures across many models.

Let me join Matt in congratulating the Rails core dev team for achieving this milestone - roll on Rails 3!

Tags:

This post has 3 responses so far

  1. Dude, no need for the ids. Those are sooo 1.2.x.

     
  2. And we can say thanks to John Barnette. He is the author of the previous plugin Rathole (http://svn.geeksomnia.com/rathole/trunk/README) who do exactly that. And Rails decided to integrate it later.

    Thanks John.

     
  3. this feature have been available for a long time in the symfony php framework, which was inspired from rails.

     

Sponsored Links

Leave a response

You are not logged in, log in with your SitePoint Forum username and password.

-OR- Post Anonymously

* Make sure any code samples are escaped (i.e. ‘<b>’ becomes ‘&lt;b&gt;’).

If not logged in, your comments will be placed in a moderation queue. This means your comment may not appear until one of our moderators approves it.

SitePoint Marketplace

Buy and sell Websites, templates, domain names, hosting, graphics and more.

Logo Design, Web page Design and more!

99designs

  • Custom logo designs created ‘just for you’.
  • Pick the design you like best.
  • Only pay if you’re satisfied with the result.