How do I sort an activerecord result set on a i18n translated column?
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 acti开发者_开发知识库verecord 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.
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}
精彩评论