On Rails using associations you can have something like this: Category.find(1).articles #=> Array of Article objects So if that returns an array how is something like this possible? Category.find(1).articles.find_by_author_id(1) Instead of calling the method "find_by_author_id" from the Article class shouldn''t Ruby have called it from the Array class? Thanks in advance --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
toulax wrote:> On Rails using associations you can have something like this: > > Category.find(1).articles #=> Array of Article objects > > So if that returns an array how is something like this possible? > > Category.find(1).articles.find_by_author_id(1) > > Instead of calling the method "find_by_author_id" from the Article > class shouldn''t Ruby have called it from the Array class?Ruby is better OO than OO. articles is both an Array and a live link between a Category and the Article class. So the Article class can do a method_missing() to concoct a find_by_whatever_column, and the articles link bequeaths to it a filter restricting the results to the current Category. Good luck finding stuff like this in all the OO books out there... -- Phlip http://www.oreilly.com/catalog/9780596510657/ ^ assert_xpath http://tinyurl.com/yrc77g <-- assert_latest Model --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Philip, Thanks, I figured about the method_missing part, but how does Ruby know when to use Array and when to use Article in this case. Better yet, how could I reproduce this behavior? Thanks On Sep 9, 1:45 pm, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> toulax wrote: > > On Rails using associations you can have something like this: > > > Category.find(1).articles #=> Array of Article objects > > > So if that returns an array how is something like this possible? > > > Category.find(1).articles.find_by_author_id(1) > > > Instead of calling the method "find_by_author_id" from the Article > > class shouldn''t Ruby have called it from the Array class? > > Ruby is better OO than OO. > > articles is both an Array and a live link between a Category and the > Article class. So the Article class can do a method_missing() to > concoct a find_by_whatever_column, and the articles link bequeaths to > it a filter restricting the results to the current Category. > > Good luck finding stuff like this in all the OO books out there... > > -- > Phliphttp://www.oreilly.com/catalog/9780596510657/ > ^ assert_xpathhttp://tinyurl.com/yrc77g <-- assert_latest Model--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
toulax wrote:> Thanks, I figured about the method_missing part, but how does Ruby > know when to use Array and when to use Article in this case.They are both. The capacities of Array and Article are merged together. Try this: def what?(anObject, aint = self) p anObject.class.name puts (anObject.public_methods - aint.public_methods).sort end Call what?cat.articles, and it will probably report that articles is an Array. Then call what?cat.articles, [], and it might report a bunch of extra methods that articles added to the array.> Better yet, how could I reproduce this behavior?Look up Ruby metaprogramming, including the chapter called IIRC "write code with code" in the book /Rails Recipes/. -- Phlip http://www.oreilly.com/catalog/9780596510657/ "Test Driven Ajax (on Rails)" assert_xpath, assert_javascript, & assert_ajax --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi -- On Sun, 9 Sep 2007, toulax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > On Rails using associations you can have something like this: > > Category.find(1).articles #=> Array of Article objects > > So if that returns an array how is something like this possible? > > Category.find(1).articles.find_by_author_id(1) > > Instead of calling the method "find_by_author_id" from the Article > class shouldn''t Ruby have called it from the Array class?You don''t call methods from a class; you send messages to objects, and the object looks for the method in the classes and modules in its lookup path. That sounds like splitting hairs, but it''s an important conceptual and technical difference. You''ve got an object that reports its class as Array -- but in Ruby, the class of an object does not definitively tell you what the object can do. In this particular case, the object has a method_missing hook that intercepts the message "find_by_author_id" and does something useful with it. So that articles collection is an Array, as indicated by its class, but the main thing is that it''s an object to which you can send certain messages and get a result. It doesn''t much matter what it says its class is, since class membership doesn''t govern an object''s behavior. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.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 -~----------~----~----~----~------~----~------~--~---
Ruby classes are open. Active support adds various methods to the array class including the various finds -- 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 -~----------~----~----~----~------~----~------~--~---
I see now that the method is really being called on the Array, but it still seems quite hard to grasp so I''ll have to look further into metaprogramming. Thanks everyone On Sep 9, 1:36 pm, "tou...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <tou...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Rails using associations you can have something like this: > > Category.find(1).articles #=> Array of Article objects > > So if that returns an array how is something like this possible? > > Category.find(1).articles.find_by_author_id(1) > > Instead of calling the method "find_by_author_id" from the Article > class shouldn''t Ruby have called it from the Array class? > > Thanks in advance--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---