How do I sort an activerecord result set on a i18n translated column?

Posted by PlanetMaster on Stack Overflow See other posts from Stack Overflow or by PlanetMaster
Published on 2010-05-20T21:58:30Z Indexed on 2010/05/20 22:00 UTC
Read the original article Hit count: 164

Filed under:

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.

© Stack Overflow or respective owner

Related posts about ruby-on-rails