map_by_method now works with ActiveRecord associations

Posted by Dr Nic on August 12, 2007

I was always annoyed that map_by_method was broken for ActiveRecord has_many associations. 6 mths later I finally fixed it.

That’s the magic of Open Source Software. [/end sarcasm]

So now, the following example works like it should:

$ gem install map_by_method
$ console
> require 'map_by_method'  # stick this in your environment.rb for Rails
> user = User.find_by_name "Dr Nic"
> user.companies.map_by_name
=> ['Dr Nic Academy', 'Dr Nic Institute of Being Silly']
> user.companies.map_by_id_and_name
=> [[1, 'Dr Nic Academy'], [9, 'Dr Nic Institute of Being Silly']]

Recap: why use map_by_method?

Try the following example:

> user.companies.map_by_employees.flatten
=> list of all employees of user

Versus:

> user.companies.map { |company| company.employees}.flatten
or
> user.companies.map(&:employees).flatten

Or compare:

> user.companies.map_by_id_and_name
=> [[1, 'Dr Nic Academy'], [9, 'Dr Nic Institute of Being Silly']]

Versus:

> user.companies.map { |company| [company.id, company.name]}

That is, it looks and feels just like ActiveRecord’s #find method, with its find_by_first_name_and_last_name magic.

Summary

No {, }, |, &, or : required. Just clean method names.

Bonus other gem

In the spirit of ActiveRecord hacks, there is to_activerecord:

$ gem install to_activerecord
$ console
> require 'to_activerecord'  # stick this in your environment.rb for Rails
> [1,2,3].to_user
=> [list of User with id's 1,2,3]

To me, this suffix operator reads cleaner than the traditional:

> User.find([1,2,3])

For example, if you want to perform an operation on the list of Users:

> ids = [1,2,3]
> ids.to_user.map_by_name
=> ['Dr Nic', 'Banjo', 'Nancy']

Versus:

> User.find(ids).map_by_name

One year on the InterTubes

Posted by Dr Nic on August 03, 2007

Dumping thoughts onto the InterTubes, aka blogging, is fun. And I’ve been doing it 1 year now.

Its also challenging.

Its like inviting people over for dinner - you have to clean up your house so they get a completely false impression of how you normally live your life.

Same with code - pasting it into blog articles or releasing projects makes you work harder to clean up code.

Same with the article - like rewriting paragraphs and sentences so they read better.

Why?

Remember the I Hired Jeff Clark site from a year ago?

I started this blog a year ago with the idea of it being an Online CV - I’d write clever things, people would comment, and employers would pick me instead of someone else.

Feedburner

Click through for fancy dynamic flash graph [1]

Leaving Tele2

The whole time I’ve been overseas working for Tele2 - a Swedish telephone company, developing their billing systems. No Ruby code anywhere. Lots of perl, some Java, and lots of an internal proprietary language. It hurts to work on that environment.

And today is my last day, so now I can pursue Ruby/Rails with full-time reckless abandon.

Starting with Dr Nic Academy.

What’s left to blog about?

In the last 12 mths, I think I’ve covered a fair bit of (seemingly random) ground: composite keys, magic models, javascript, radrails fixups, newgem, and lately openid.

In the future, the following stuff intrigues me, and it’d be fun to explore:

  1. Social OS - when I use flickr, I add contacts/friends/family and I add photos. When I visit your fancy new Web2.0 site, how can I import this profile information and automatically find all my friends or invite them to use your site? I envisage this built on OpenID, where every app is both an OpenID consumer (you login with OpenID) and possibly an OpenID provider (you can use your account page as a login to other apps).
  2. Mongrel handlers - sexy integration of handlers into the Rails code base, so they are automatically picked up when the mongrel servers are started. I haven’t looked into this at all, so that sentence mightn’t make any sense.
  3. Caching - nothing fancy, I just haven’t figured out how to use it yet. Anyone that used MyConfPlan during RailsConf might have guessed this.
  4. Javascript widget generator - generators get me from “idea” to “ooh that’s pretty” much faster, and avoid the “oh I can’t be bothered setting up the code base” step. I want to write a bunch of widgets for the RoR Oceania blog, to pull data from the RoR Oceania facebook API, so hopefully I can extract some base code into a generator.

As always, I’m completely happy for someone else to tackle these things first. So get cracking :)


[1] The XML for this
Maani graph is mashed from the raw feedburner API via this Ruby CGI script. If you want to use it, you need to turn on the Feedburner API first, and change the start date within the code (currently set to 1/8/2006)

That is, the data goes from feedburner XML to maani XML.

Rails on the Fairway - Matt Allen

Posted by Dr Nic on August 02, 2007

Matt Allen works at iseekgolf.com which was ported from PHP to Rails. It has 130 models. It does lots of caching.

And Matt did a talk on it, and its been Viddler-ized by Lachie Cox. Look out for the very cool graphical intro.

Oceania Ruby on Rails

The growing Rails crowd in Australia now has a new blog/site for announcements, plus its famous schooner facebook site.

I’m certainly looking forward to returning home at the end of September and coming down to Sydney to meet the guys during the Web Directions South conference (my workshop here).