Find on a chained relationship

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

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.

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”:

Simon

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

-Anthony

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.

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

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)

you could just do

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

regards,

j