Here''s a little method module ActiveRecord class Base def self.[](id) find(id) end end end So you can write MyModel[12] as an equivalent to MyModle.find(12) Just a little sugar, nothing else really. Is anyone aware of why that would be ill-advised? It seems to work with my extremely limited testing. It''s non-orthogonal with respect to association proxies, so maybe that''s bad: MyModel[12] is the same as MyModel.find(12) but MyModel.friends[12] is not the same as MyModel.friends.find(12) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
jamis buck has suggested the same thing: http://weblog.jamisbuck.org/2007/4/4/activerecord-base-find-shortcut On Oct 14, 12:49 pm, "Bob Showalter" <showa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Here''s a little method > > module ActiveRecord > class Base > def self.[](id) > find(id) > end > end > end > > So you can write > > MyModel[12] > > as an equivalent to > > MyModle.find(12) > > Just a little sugar, nothing else really. Is anyone aware of why that > would be ill-advised? It seems to work with my extremely limited > testing. > > It''s non-orthogonal with respect to association proxies, so maybe that''s bad: > > MyModel[12] is the same as MyModel.find(12) > > but > > MyModel.friends[12] is not the same as MyModel.friends.find(12)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
My instinct is to say, "yes, but what happens when record #5 is deleted?" There are two problems at play: 1) The use of incrementing integers for primary keys (bad, btw, we need guids much of the time) misleads us into thinking of the table as an array indexed by the primary keys. It is not. It is a collection indexed by the primary keys. 2) The use of the [] operator with integer operands likewise intuitively directs to an array. But in ruby, a[5] might be the sixth element of an array, or the value of a hash for the key 5. Since these classes are mapped to tables, and tables are certainly collections of rows, then the use of the [] operator is sensible. HOWEVER... You are not the sole maintainer of this code. Sooner or later, someone else is going to maintain it. You are changing a base behavior of the system. Be sure to leave markers. Is MyModel.friends.find even well defined? On Oct 14, 2:30 pm, jemminger <jemmin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> jamis buck has suggested the same thing:http://weblog.jamisbuck.org/2007/4/4/activerecord-base-find-shortcut > > On Oct 14, 12:49 pm, "Bob Showalter" <showa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Here''s a little method > > > module ActiveRecord > > class Base > > def self.[](id) > > find(id) > > end > > end > > end > > > So you can write > > > MyModel[12] > > > as an equivalent to > > > MyModle.find(12) > > > Just a little sugar, nothing else really. Is anyone aware of why that > > would be ill-advised? It seems to work with my extremely limited > > testing. > > > It''s non-orthogonal with respect to association proxies, so maybe that''s bad: > > > MyModel[12] is the same as MyModel.find(12) > > > but > > > MyModel.friends[12] is not the same as MyModel.friends.find(12)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Marnen Laibow-Koser
2007-Oct-17 14:25 UTC
Re: Why not [] as ActiveRecord::Base class method?
Student wrote:> My instinct is to say, "yes, but what happens when record #5 is > deleted?"Presumably, MyModel[5] would then simply find nothing.> There are two problems at play: > 1) The use of incrementing integers for primary keys (bad, btw, we > need guids much of the time)Why?> misleads us into thinking of the table as > an array indexed by the primary keys. It is not. It is a collection > indexed by the primary keys.True, but so what? The distinction is somewhat irrelevant for the current question. [...]> You are not the sole maintainer of this code. Sooner or later, > someone else is going to maintain it. You are changing a base > behavior of the system.[...] I think it''s more correct to say that he''s *adding* a behavior to the system -- after all, .find should still work. Best, -- Marnen Laibow-Koser marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---