# Method on a array of objects from the database

**URL:** <https://www.ruby-forum.com/t/method-on-a-array-of-objects-from-the-database/121672>\
**Category:** Rails\
**Created:** [November 29, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/method-on-a-array-of-objects-from-the-database/121672 "2007-11-29T20:24:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierreva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pierreva](https://www.ruby-forum.com/u/pierreva)\
**Post date:** [November 29, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/method-on-a-array-of-objects-from-the-database/121672/1 "2007-11-29T20:24:14Z")

</div>

Hello,

I would like to know the more elegant way to add a method for an array  
of objects from the database.

Let me give an example :

Class Product  
…  
end

And in my controller :

@products = Product.find(:all)

And now I want to define a method which I could like

@products.select\_fruits

having said somewhere :

def select\_fruits  
…  
end

Thank you very much,

Pierre

---

<div class="post-metadata">

**Author:** ![pierreva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pierreva](https://www.ruby-forum.com/u/pierreva)\
**Post date:** [November 29, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/method-on-a-array-of-objects-from-the-database/121672/2 "2007-11-29T20:49:26Z")

</div>

On Nov 29, 2007, at 11:23 AM, Pierre V. wrote:

> end  
> Is you question about where those methods should go? They can, and  
> usually should, go in the model.

class Product

def self.find\_all  
find(:all, …)  
end

def self.select\_fruits(parameters)  
find(:all, :conditions…  
end

end

— controller –

fruits = Product.select\_fruits(:fruit\_type = params[:…])

–  
def gw  
acts\_as\_n00b  
writes\_at(www.railsdev.ws)  
end

---

<div class="post-metadata">

**Author:** ![pierreva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pierreva](https://www.ruby-forum.com/u/pierreva)\
**Post date:** [November 29, 2007, 8:53pm UTC](https://www.ruby-forum.com/t/method-on-a-array-of-objects-from-the-database/121672/3 "2007-11-29T20:53:36Z")

</div>

Thank you for your answer but my question is how to add method to an  
array of @products.

I have : @products.find(:all)

Then, if I want to select only fruits from @products, I could do :  
@products.select {…}  
And it’s this select that I want to put in a method select\_fruits so  
that I could do :

@products.select\_fruits which corresponds to @products.select { … }

Thank you,  
Pierre

---

<div class="post-metadata">

**Author:** ![pierreva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pierreva](https://www.ruby-forum.com/u/pierreva)\
**Post date:** [November 29, 2007, 10:37pm UTC](https://www.ruby-forum.com/t/method-on-a-array-of-objects-from-the-database/121672/4 "2007-11-29T22:37:19Z")

</div>

> @products = Product.find(:all)
> 
> And now I want to define a method which I could like
> 
> @products.select\_fruits
> 
> having said somewhere :

module Foo  
def select\_fruits  
# some code here  
end  
end

# meanwhile… back on the farm…

@products = Product.find(:all)  
@products.extend Foo

=================== Or even better… =========================  
class Product  
belongs\_to :fruit, :extends =\> Foo  
end

Look for “Association extensions” within the Rails api…

hth…

ilan

---

<div class="post-metadata">

**Author:** ![pierreva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pierreva](https://www.ruby-forum.com/u/pierreva)\
**Post date:** [November 30, 2007, 10:20am UTC](https://www.ruby-forum.com/t/method-on-a-array-of-objects-from-the-database/121672/5 "2007-11-30T10:20:54Z")

</div>

Thanks you ! It’s great !  
Pierre

---

<div class="post-metadata">

**Author:** ![pierreva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pierreva](https://www.ruby-forum.com/u/pierreva)\
**Post date:** [November 29, 2007, 9:34pm UTC](https://www.ruby-forum.com/t/method-on-a-array-of-objects-from-the-database/121672/6 "2007-11-29T21:34:40Z")

</div>

One way is to make your own finder directly on the model

class Product \< ActiveRecord::Base

def self.find\_all\_fruits  
self.find :all, :condition =\> ["category = “fruit”]  
end

end

Although you don’t have to bother if you can tell by looking at a  
field.  
For example, if you have a “category” column that shows “fruit”, you can  
just use a dynamic finder for that

@fruit = Product.find\_all\_by\_category(“fruit”)  
@fruit\_in\_stock =  
Product.find\_all\_by\_category\_and\_available(“fruit”,true)

You should be letting your database do the work of filtering records, as  
that’s its job. You don’t want to iterate and reject / filter records in  
Ruby. It’s much slower. It looks cool but it’s not worth it.
