# All elements of an array

**URL:** <https://www.ruby-forum.com/t/all-elements-of-an-array/56997>\
**Category:** Rails\
**Created:** [April 6, 2006, 12:18pm UTC](https://www.ruby-forum.com/t/all-elements-of-an-array/56997 "2006-04-06T12:18:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@Daan](https://www.ruby-forum.com/u/Daan)\
**Post date:** [April 6, 2006, 12:18pm UTC](https://www.ruby-forum.com/t/all-elements-of-an-array/56997/1 "2006-04-06T12:18:17Z")

</div>

Hi there,

i have a model house and a model owner

A house can have many owners.

Now i want to find all owners of a specific street and i want to list  
them

So i do:

houses = House.find(:all, :conditions =\> ‘street LIKE “%foo%”’)

So now i got an array of all houses in that street

@owners = houses[0].owner i’ve got all the owners of the first house.  
but i want from all houses so something like this:

@owners = houses[:all].owner

Does anyone know how to do this?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Daan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@Daan](https://www.ruby-forum.com/u/Daan)\
**Post date:** [April 6, 2006, 12:21pm UTC](https://www.ruby-forum.com/t/all-elements-of-an-array/56997/2 "2006-04-06T12:21:09Z")

</div>

piece of cake for ruby 🙂

owners = houses.map do |house| house.owner end

---

<div class="post-metadata">

**Author:** ![Daan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@Daan](https://www.ruby-forum.com/u/Daan)\
**Post date:** [April 6, 2006, 12:25pm UTC](https://www.ruby-forum.com/t/all-elements-of-an-array/56997/3 "2006-04-06T12:25:57Z")

</div>

Peter E. wrote:

> piece of cake for ruby 🙂
> 
> owners = houses.map do |house| house.owner end

Thnx for the reply. I forgot something to tell 🙂

i use

@owners = houses[0].owner.find(:all, :conditions =\> ‘age \> 30’, :limit  
=\> 100)

i guess in your way i could do:

owners = houses.map do |house| house.owner.find(:all, :conditions =\>  
‘age \> 30’, :limit =\> 100) end

but then the limit will not work. And in the end i want to use paginate  
as well… 🙂

You got a solution?

---

<div class="post-metadata">

**Author:** ![Daan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@Daan](https://www.ruby-forum.com/u/Daan)\
**Post date:** [April 6, 2006, 5:49pm UTC](https://www.ruby-forum.com/t/all-elements-of-an-array/56997/4 "2006-04-06T17:49:04Z")

</div>

On Apr 6, 2006, at 3:25 AM, Daan wrote:

> but then the limit will not work. And in the end i want to use  
> paginate  
> as well… 🙂
> 
> You got a solution?

Something like this should work:

owners = Owner.find(:all, :include =\> :house, :conditions =\>  
‘houses.street LIKE ‘%foo%’ AND owners.age \> 30’, :limit =\> 100)

Hope that helps.

Ryan

---

<div class="post-metadata">

**Author:** ![Daan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@Daan](https://www.ruby-forum.com/u/Daan)\
**Post date:** [April 6, 2006, 6:38pm UTC](https://www.ruby-forum.com/t/all-elements-of-an-array/56997/5 "2006-04-06T18:38:47Z")

</div>

On Apr 6, 2006, at 8:48 AM, Ryan B. wrote:

> owners = Owner.find(:all, :include =\> :house, :conditions =\>  
> ‘houses.street LIKE ‘%foo%’ AND owners.age \> 30’, :limit =\> 100)

Sorry, forgot to use double quotes. That should be:  
owners = Owner.find(:all, :include =\> :house, :conditions =\>  
“houses.street LIKE ‘%foo%’ AND owners.age \> 30”, :limit =\> 100)

And remember to sanitize any user input (for example, if “foo” or  
“30” came from params).

Ryan

---

<div class="post-metadata">

**Author:** ![Daan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@Daan](https://www.ruby-forum.com/u/Daan)\
**Post date:** [April 6, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/all-elements-of-an-array/56997/6 "2006-04-06T19:06:19Z")

</div>

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Daan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@Daan](https://www.ruby-forum.com/u/Daan)\
**Post date:** [April 6, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/all-elements-of-an-array/56997/7 "2006-04-06T19:18:14Z")

</div>

Daan wrote:

> Hi there,
> 
> i have a model house and a model owner
> 
> A house can have many owners.
> 
> Now i want to find all owners of a specific street and i want to list them

You can use a has\_many :through to avoid all that mucking about with  
conditions:

class Street \< ActiveRecord::Base  
has\_many :houses  
has\_many :owners, :through =\> :houses  
end

all\_owners = a\_street.owners

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
