find on a chained relationship

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

Anthony Walsh wrote:

Anthony Walsh 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