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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hello Anthony,> 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 > endMaybe something like that : cities = Country.find(1).cities @people = Person.find(:all, :conditions => ['city_id IN (?)', cities]) -- Jean-François. -- À la renverse. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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": http://www.rubyonrails.org/api/classes/ActiveRecord/Associations/ClassMethods.html Simon Anthony Walsh wrote:> 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 > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks, Simon''s and Jean''s suggesetions both worked. -Anthony -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Simon Pasquier 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": > http://www.rubyonrails.org/api/classes/ActiveRecord/Associations/ClassMethods.html > > SimonIs 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. (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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---