Feature Idea: ActiveRecord::Base[id]

I've got a patch working for this and wanted to know the core-team's opinion on this feature-add. I often find myself wanting to do finds with id's and having the natural urge do to this.

User[12] #=> User.find(12)

Asking the User < ActiveRecord::Base object to give me an "index" on it... aka, the primary key... aka, the id.

Any interest in this as a nice little shorthand? I'm asking here before making an actual-patch because I know that changes to ActiveRecord API are serious and I wanted to get opinions first.

-hampton.

In case you didn’t see it Jamis wrote about this a while back: http://weblog.jamisbuck.org/2007/4/4/activerecord-base-find-shortcut

::Jack Danger

I’ve sometimes been using it even before Jamis blogged about it and, naturally, I like it. http://weblog.jamisbuck.org/2007/4/4/activerecord-base-find-shortcut

But would it cause confusion, chaos? Dunno.

It's a widely used technique already actually.

Check Buckblog: ActiveRecord::Base#find shortcut and comment section there to see why it's not in core.

Thanks, Pratik

Hm, I’ve never really read the comments. There are some good arguments in there!

I’m also in the “don’t add it” camp. It could really lead to confusion.

Yeah, same here.

But it makes a great plugin! :wink:

http://projects.wh.joshpeek.com/browser/plugins/find_shortcut

-1 Don't add this to core, keep it as a plugin.

For example, I use a plugin called "acts_as_lookup" in which you can do things like State["NY","California"] and State[5] where it will return the matching records.

IMHO defining the method on AR objects should be done in plugins, or it should be gone all out with configurability options.