if class A has_many class b. and class B has_many class C. when doing a find on class A, will my model have class c information? like.... class maincategory class subcategory maincategory_id class bottom. subcategory_id also, is it possible to get a model from the bottom class, and find out whats its main category is? THANKS! -- 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 -~----------~----~----~----~------~----~------~--~---
Taylor Strait
2006-Dec-11 01:04 UTC
Re: NOOB question about DB relationship and using find!
Lets make it a little less abstract. Class Country has_many :states Class State belongs_to :country has_many :cities Class City belongs_to :state You can grab an array of related cities from the Country by doing: Country.find(id).state.cities You can go backwards, too: City.find(id).state.country Keep in mind the proper pluralization according to has_many or belongs_to. Did that answer your question? -- 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 -~----------~----~----~----~------~----~------~--~---
Taylor Strait
2006-Dec-11 01:09 UTC
Re: NOOB question about DB relationship and using find!
> You can grab an array of related cities from the Country by doing: > Country.find(id).state.citiesSlight mistake here. You need to identify the state because a country has_many states. So the proper way would be: Country.find(id).state[array_index_number].cities You can only go from 1:1 or 1:M, never M:M. This is why you cannot do: Country.find(id).states.cities -- 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 -~----------~----~----~----~------~----~------~--~---
thank you taylor for the explanation! -- 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 -~----------~----~----~----~------~----~------~--~---
ok, now that i tried it out, i got some more questions please. to expand on this "Country.find(id).state[array_index_number].cities" how can i return all the city names in a view for a country? basically i will need a hash where in each position is the state name linked to an array of city names for that state? is it possible to do the find in the cities table like Cities.find( :all, :conditions => ''city.state.country_id => ?'',params[:selectedCountry] ) ? -- 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 -~----------~----~----~----~------~----~------~--~---
Taylor Strait
2006-Dec-11 17:41 UTC
Re: NOOB question about DB relationship and using find!
> I''ve never tried a reverse join like you suggest. Here is the ''vanilla'' way of doing it:The find method calls a Class, so it will be singular like: City.find, Country.find, etc. @country = Country.find(id_number, :include => {:states => :cities}} will create a massive nested object with all states and cities properly organized. To see the hash add <%= debug(@country) %> to your view. But remember you cannot use ActiveRecord class methods on arrays so you will have to iterate like: <% for state in @country.states %> #iterate through states one at a time <% for city in state.cities %> #iterate through each city one at a time <%= city.name %> #show each city object''s name <% end %> <% end %> If you only had one layer of 1:M you could simply collect the find results into an array and then spit out a string, "Atlanta, Macon, Savannah": city_names = State.find(id_number).cities.collect {|c| c.name }.join(", ") ALL THIS CODE IS UNTESTED! -- 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 -~----------~----~----~----~------~----~------~--~---
nice! thanks Taylor! works awesomely! @country = Country.find(id_number, :include => {:states => :cities}} Taylor Strait wrote:> >> I''ve never tried a reverse join like you suggest. Here is the ''vanilla'' way of doing it: > > The find method calls a Class, so it will be singular like: City.find, > Country.find, etc. > > @country = Country.find(id_number, :include => {:states => :cities}} > > will create a massive nested object with all states and cities properly > organized. To see the hash add <%= debug(@country) %> to your view. > But remember you cannot use ActiveRecord class methods on arrays so you > will have to iterate like: > > <% for state in @country.states %> #iterate through states one at a time > <% for city in state.cities %> #iterate through each city one at a > time > <%= city.name %> #show each city object''s name > <% end %> > <% end %> > > If you only had one layer of 1:M you could simply collect the find > results into an array and then spit out a string, "Atlanta, Macon, > Savannah": > > city_names = State.find(id_number).cities.collect {|c| c.name }.join(", > ") > > ALL THIS CODE IS UNTESTED!-- 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 -~----------~----~----~----~------~----~------~--~---