# Adding methods to Collections

**URL:** <https://discuss.rubyonrails.org/t/adding-methods-to-collections/24682>\
**Category:** rubyonrails-talk\
**Created:** [February 10, 2008, 8:21pm UTC](https://discuss.rubyonrails.org/t/adding-methods-to-collections/24682 "2008-02-10T20:21:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonas\_Schneider](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jonas\_Schneider](https://discuss.rubyonrails.org/u/Jonas_Schneider)\
**Post date:** [February 10, 2008, 8:21pm UTC](https://discuss.rubyonrails.org/t/adding-methods-to-collections/24682/1 "2008-02-10T20:21:35Z")

</div>

Hello,

I´m planning to create a room managaement system in Rails. I have a collection of rooms, like this:

@rooms = Room.find :all

Then I want to write:

@rooms.to\_svg # (or any other method name)

to convert this collection of rooms into SVG. Now the question: Where do I have to define those methods? I could write

def @rooms.to\_svg

but that would be only for this collection; if I do Room.find :blah, I want to be able to do the same thing.

Thanks, Jonas

---

<div class="post-metadata">

**Author:** ![Gregory\_Seidman1](https://avatars.discourse-cdn.com/v4/letter/g/a88e57/32.png) [@Gregory\_Seidman1](https://discuss.rubyonrails.org/u/Gregory_Seidman1)\
**Post date:** [February 10, 2008, 8:29pm UTC](https://discuss.rubyonrails.org/t/adding-methods-to-collections/24682/2 "2008-02-10T20:29:04Z")

</div>

> Hello,
> 
> I?m planning to create a room managaement system in Rails. I have a collection of rooms, like this:
> 
> @rooms = Room.find :all
> 
> Then I want to write:
> 
> @rooms.to\_svg # (or any other method name)
> 
> to convert this collection of rooms into SVG. Now the question: Where do I have to define those methods? I could write
> 
> def @rooms.to\_svg
> 
> but that would be only for this collection; if I do Room.find :blah, I want to be able to do the same thing.

class Room \< ActiveRecord::Base &nbsp;&nbsp;module MyCollectionMethods &nbsp;&nbsp;&nbsp;&nbsp;def to\_svg &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#... &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;def self.find(\*args) &nbsp;&nbsp;&nbsp;&nbsp;result = super(\*args) &nbsp;&nbsp;&nbsp;&nbsp;if args[0] == :all || Array === args[0] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.extend(MyCollectionMethods) &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;result &nbsp;&nbsp;end

end

Note: off the cuff and untested

> Thanks, Jonas

--Greg

---

<div class="post-metadata">

**Author:** ![Jonas\_Schneider](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jonas\_Schneider](https://discuss.rubyonrails.org/u/Jonas_Schneider)\
**Post date:** [February 10, 2008, 9:03pm UTC](https://discuss.rubyonrails.org/t/adding-methods-to-collections/24682/3 "2008-02-10T21:03:21Z")

</div>

Yeah, works great =)

But why the check if args[0] == :all || Array === args[0] ?

Greets Jonas

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [February 10, 2008, 9:09pm UTC](https://discuss.rubyonrails.org/t/adding-methods-to-collections/24682/4 "2008-02-10T21:09:04Z")

</div>

> > class Room \< ActiveRecord::Base module MyCollectionMethods &nbsp;&nbsp;&nbsp;def to\_svg &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#... &nbsp;&nbsp;&nbsp;end end
> > 
> > def self.find(\*args) &nbsp;&nbsp;&nbsp;result = super(\*args) &nbsp;&nbsp;&nbsp;if args[0] == :all || Array === args[0] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.extend(MyCollectionMethods) &nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;result end
> > 
> > end
> 
> Yeah, works great =)
> 
> But why the check if args[0] == :all || Array === args[0] ?

So that you don't add the collection methods when find is not  
returning an Array (ie if you're doing find(some\_id) or find :first)

Fred

---

<div class="post-metadata">

**Author:** ![Jonas\_Schneider](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jonas\_Schneider](https://discuss.rubyonrails.org/u/Jonas_Schneider)\
**Post date:** [February 10, 2008, 9:19pm UTC](https://discuss.rubyonrails.org/t/adding-methods-to-collections/24682/5 "2008-02-10T21:19:23Z")

</div>

Ah, OK. Thanks!

--Jonas

---

<div class="post-metadata">

**Author:** ![Bala\_Paranj](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@Bala\_Paranj](https://discuss.rubyonrails.org/u/Bala_Paranj)\
**Post date:** [February 10, 2008, 10:26pm UTC](https://discuss.rubyonrails.org/t/adding-methods-to-collections/24682/6 "2008-02-10T22:26:27Z")

</div>

We could do:

Unless result.is-array Result.extend End Result

Any thoughts?

[Http://www](Http://www).rubyplus.org Free Ruby & Rails screencasts
