to_activerecord

Get Version

0.2.0

→ 7.to_car == Car.find(7)

What

Find ActiveRecord instances via their ID value.

So, instead of Page.find(37) you can do:

>> 37.to_page
=> #<Page:0x345e39c @attributes={"body"=>"Ooh, a new page", 
  "title"=>"Dr Nic's first page", "id"=>"37", "user_id"=>"1"}>

Used in conjunction with the map_by_method gem:

>> [37, 38].map_by_to_page
=> [#<Page:0x345e39c @attributes={"body"=>"Ooh, a new page", 
  "title"=>"Dr Nic's first page", "id"=>"37", "user_id"=>"1"}>,
  #<Page:0x345e91d @attributes={"body"=>"Another page", 
    "title"=>"Dr Nic's second page", "id"=>"38", "user_id"=>"1"}>

Installing

sudo gem install to_activerecord

Why?

Building.find(45) is procedural syntax; like building_find(45). We like messages, not procedures. They allow you to flow from one object to the next. 45.to_building45.to_building.tenants, etc. It seems to read nicely to me.

How?

The Fixnum (integer) class catches any methods like to_abcd and checks if Abcd class exists and then calls the Abcd.find method with the target integer as a parameter. Easy peasy.

Forum

http://groups.google.com/group/drnicutilities

How to submit patches

Read the 8 steps for fixing other people’s code and for section 8b: Submit patch to Google Groups, use the Google Group above.

The trunk repository is svn://rubyforge.org/var/svn/drnicutilities/to_activerecord/trunk for anonymous access.

License

This code is free to use under the terms of the MIT license.

Contact

Comments are welcome. Send an email to Dr Nic Williams.

Dr Nic, 17th June 2007
Theme extended from Paul Battley