I have three models: Language, Country and City. A country has a Language and a language belongs to many countries. A country has many cities and a city belongs to a country. When I use Active(Scaffold) and I want to edit/insert a City, I must select a Country. Is possible that when I must select a country, in the list of countries appear the Country + the language. For example: USA-English France-French England-English ... instead of: USA France England ... 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 -~----------~----~----~----~------~----~------~--~---
Sure, I assume you do a query for the countries -> @countries = Country.find (:all) for starters do this @countries = Country.find(:all, :include => ''languages'') which will get the languages in the same query. Now, in the select you have options_for_select=[array] so you have to build an array that looks like this ["USA - English","France-French","England-English",...] I would suggest that you create a helper method def country_options(country_array) reponse_array = [] country_array.each do |country| response_array << country.name + " - " + country.language.name end return reponse_array end reponse array wil now look like this ["USA - English","France-French","England-English",...] so you can do <%= select_tag(:country, options_for_select(country_options(@countries))) %> and that will give you the select you wanted. On 4/26/07, Mike <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > I have three models: Language, Country and City. > > A country has a Language and a language belongs to many countries. > A country has many cities and a city belongs to a country. > > When I use Active(Scaffold) and I want to edit/insert a City, I must > select a Country. Is possible that when I must select a country, in the > list of countries appear the Country + the language. > > For example: > > USA-English > France-French > England-English > ... > > instead of: > > USA > France > England > ... > > > 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 -~----------~----~----~----~------~----~------~--~---
Hi -- On 4/26/07, Ivor Paul <ivorpaul-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sure, I assume you do a query for the countries -> @countries > Country.find(:all) > > for starters do this > @countries = Country.find(:all, :include => ''languages'') which will get the > languages in the same query. > > Now, in the select you have options_for_select=[array] so you have to build > an array that looks like this ["USA - > English","France-French","England-English",...]I believe you''d actually need something like: [ ["USA-English", "USA"], ["France-French", "French"], ... ] so that you''re providing both a label and a value for each option.> I would suggest that you create a helper method > > def country_options(country_array) > reponse_array = [] > country_array.each do |country| > response_array << country.name + " - " + country.language.name > end > return reponse_array > endYou''ve just reimplemented map :-) You can let Ruby do more of the work (untested): def country_options(countries) countries.map do |country| [country.name + ''-'' + country.language.name, country.name] end end You could also use country.id instead of country.name for the value, which might make it slightly easier to pull the record later. David -- Upcoming Rails training by Ruby Power and Light: Four-day Intro to Intermediate May 8-11, 2007 Edison, NJ http://www.rubypal.com/events/05082007 --~--~---------~--~----~------------~-------~--~----~ 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 David. Yes, I should have done the [text_value,id] - forgot about that :) map! nice, yeah, thanks for the tip. So map creates an array populated with the result of the block for each element in the array that is being mapped? the result is then the array containing the results for each of the original elements. thanks again Ivor On 4/26/07, David A. Black <dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org> wrote:> > > Hi -- > > On 4/26/07, Ivor Paul <ivorpaul-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Sure, I assume you do a query for the countries -> @countries > > Country.find(:all) > > > > for starters do this > > @countries = Country.find(:all, :include => ''languages'') which will get > the > > languages in the same query. > > > > Now, in the select you have options_for_select=[array] so you have to > build > > an array that looks like this ["USA - > > English","France-French","England-English",...] > > I believe you''d actually need something like: > > [ ["USA-English", "USA"], ["France-French", "French"], ... ] > > so that you''re providing both a label and a value for each option. > > > I would suggest that you create a helper method > > > > def country_options(country_array) > > reponse_array = [] > > country_array.each do |country| > > response_array << country.name + " - " + country.language.name > > end > > return reponse_array > > end > > You''ve just reimplemented map :-) You can let Ruby do more of the > work (untested): > > def country_options(countries) > countries.map do |country| > [country.name + ''-'' + country.language.name, country.name] > end > end > > You could also use country.id instead of country.name for the value, > which might make it slightly easier to pull the record later. > > > David > > -- > Upcoming Rails training by Ruby Power and Light: > Four-day Intro to Intermediate > May 8-11, 2007 > Edison, NJ > http://www.rubypal.com/events/05082007 > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---