# Find on a chained relationship

**URL:** <https://www.ruby-forum.com/t/find-on-a-chained-relationship/78553>\
**Category:** Rails\
**Created:** [December 6, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/find-on-a-chained-relationship/78553 "2006-12-06T20:28:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [December 6, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/find-on-a-chained-relationship/78553/1 "2006-12-06T20:28:07Z")

</div>

I have a one-to-many relationship from person to city. I also have  
another one-to-many relationship from city-to-county. Is there a way to  
find all people in a county from the people class (i.e. @people =  
People.City.County.find(1)?

Below are my models for the three classes.

class Person \< ActiveRecord::Base  
belongs\_to : city  
end

class City \< ActiveRecord::Base  
has\_many :people  
belongs\_to :county  
end

class County \< ACtiveRecord::Base  
has\_many :cities  
end

-Anthony

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [December 6, 2006, 9:08pm UTC](https://www.ruby-forum.com/t/find-on-a-chained-relationship/78553/2 "2006-12-06T21:08:06Z")

</div>

Hello Anthony,

> class City \< ActiveRecord::Base  
> has\_many :people  
> belongs\_to :county  
> end
> 
> class County \< ACtiveRecord::Base  
> has\_many :cities  
> end

Maybe something like that :

cities = Country.find(1).cities  
@people = Person.find(:all, :conditions =\> [‘city\_id IN (?)’, cities])

```
-- Jean-FranÃ§ois.

```

–  
Ã€ la renverse.

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [December 6, 2006, 10:00pm UTC](https://www.ruby-forum.com/t/find-on-a-chained-relationship/78553/3 "2006-12-06T22:00:22Z")

</div>

The Rails API documentation indicates that you can go through a has\_many  
association so you should be able to write:

class Person \< ActiveRecord::Base  
belongs\_to : city  
end

class City \< ActiveRecord::Base  
has\_many :people  
belongs\_to :county  
end

class County \< ACtiveRecord::Base  
has\_many :cities  
has\_many :people, :through =\> :cities  
end

@people = County.find(:first).people

More details here in “Association Join Models”:

> **[Ruby on Rails](https://rubyonrails.org)**
>
> A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.

Simon

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [December 9, 2006, 11:28am UTC](https://www.ruby-forum.com/t/find-on-a-chained-relationship/78553/4 "2006-12-09T11:28:40Z")

</div>

Thanks, Simon’s and Jean’s suggesetions both worked.

-Anthony

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [December 9, 2006, 1:02pm UTC](https://www.ruby-forum.com/t/find-on-a-chained-relationship/78553/5 "2006-12-09T13:02:20Z")

</div>

Simon P. wrote:

> The Rails API documentation indicates that you can go through a has\_many  
> association so you should be able to write:
> 
> class Person \< ActiveRecord::Base  
> belongs\_to : city  
> end
> 
> class City \< ActiveRecord::Base  
> has\_many :people  
> belongs\_to :county  
> end
> 
> class County \< ACtiveRecord::Base  
> has\_many :cities  
> has\_many :people, :through =\> :cities  
> end
> 
> @people = County.find(:first).people
> 
> More details here in “Association Join Models”:  
> [Ruby on Rails — A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.](http://www.rubyonrails.org/api/classes/ActiveRecord/Associations/ClassMethods.html)
> 
> Simon

Is there a way to find with multiple ids?  
i.e. @people = County.find(:all, :condtions =\> “id=3 OR id=5”).people

I’ve tried this an it fails.

-Anthony

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [December 9, 2006, 4:37pm UTC](https://www.ruby-forum.com/t/find-on-a-chained-relationship/78553/6 "2006-12-09T16:37:35Z")

</div>

Anthony W. wrote:

> Is there a way to find with multiple ids?  
> i.e. @people = County.find(:all, :condtions =\> “id=3 OR id=5”).people
> 
> I’ve tried this an it fails.

when you use find (:all) you are getting always an array (empty or not),  
so you cannot do find(:all).people, since you need to ask for the  
“people” key of a given index in your array.

If you try this, everything will be fine

arr\_people = County.find(:all, :conditions =\> “id=3 OR id=5”)  
@people = arr\_people[0].people unless arr\_people.blank?

if you need to iterate over the results to find all the “people” values,  
you can just use .each over arr\_people.

and finally, if you want to get different rows given several id’s, it’s  
not necessary to use the :conditions param. Find will work fine if you  
pass an id,a list or an array of id’s.  
([Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000860))

you could just do

arr\_people = County.find (3,5)  
@people = arr\_people[0].people unless arr\_people.blank?

regards,

j
