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.


In case you didn’t see it Jamis wrote about this a while back:

::Jack Danger

I’ve sometimes been using it even before Jamis blogged about it and, naturally, I like it.


But would it cause confusion, chaos? Dunno.

It's a widely used technique already actually.

Check http://weblog.jamisbuck.org/2007/4/4/activerecord-base-find-shortcut
and comment section there to see why it's not in core.


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:


-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.