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.
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.
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”,…]
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.