views:

8

answers:

1

Hi,

I have the following line in a view:

<%= f.select(:province_id, options_from_collection_for_select(Province.find(:all, :conditions => { :country_id => @property.country_id }, :order => "provinces.name ASC"), :id, :name) %>

In the province model I have the following:

  def name
     I18n.t(super)
  end

Problem is that the :name field is translated (through the province model) and that the ordering is done by activerecord on the english name. The non-english result set can be wrongly sorted this way. We have a province in Belgium called 'Oost-Vlaanderen'. In english that is 'East-Flanders". Not good for sorting:)

I need something like this, but it does not work:

<%= f.select(:province_id, options_from_collection_for_select(Province.find(:all, :conditions => { :country_id => @property.country_id }, :order => "provinces.I18n.t(name) ASC"), :id, :name) %>

What would be the best approach to solve this? As you may have noticed, my coding knowledge is very limited, sorry for that.

+1  A: 

You need to sort the entries after they have been loaded from the database. This should probably do the trick:

Provinces.find(:all, :conditions => {:country_id => @property.country_id}).sort_by{|p| p.name}
elektronaut
yes, that did the trick, thanks!
PlanetMaster