# OOP Question

**URL:** <https://www.ruby-forum.com/t/oop-question/114200>\
**Category:** Rails\
**Created:** [September 9, 2007, 6:37pm UTC](https://www.ruby-forum.com/t/oop-question/114200 "2007-09-09T18:37:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 9, 2007, 6:37pm UTC](https://www.ruby-forum.com/t/oop-question/114200/1 "2007-09-09T18:37:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 9, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/oop-question/114200/2 "2007-09-09T18:45:41Z")

</div>

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/](http://www.oreilly.com/catalog/9780596510657/)  
^ assert\_xpath  
[O'Reilly Media - Technology and Business Training](http://tinyurl.com/yrc77g) ← assert\_latest Model

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 9, 2007, 6:55pm UTC](https://www.ruby-forum.com/t/oop-question/114200/3 "2007-09-09T18:55:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 9, 2007, 7:05pm UTC](https://www.ruby-forum.com/t/oop-question/114200/4 "2007-09-09T19:05:42Z")

</div>

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/](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 10, 2007, 7:01am UTC](https://www.ruby-forum.com/t/oop-question/114200/5 "2007-09-10T07:01:33Z")

</div>

Ruby classes are open.  
Active support adds various methods to the array class including the  
various finds

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 9, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/oop-question/114200/6 "2007-09-09T20:02:47Z")

</div>

Hi –

On Sun, 9 Sep 2007, removed\_email\_address@domain.invalid wrote:

> 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](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 10, 2007, 8:15am UTC](https://www.ruby-forum.com/t/oop-question/114200/7 "2007-09-10T08:15:59Z")

</div>

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
