Я проверил это с помощью sqlite3, и он работает.
class Category < ActiveRecord::Base
...
named_scope :ordered, lambda {|locale|
{
#:select => "categories.*, categories.name sort_name",
# For MySQL
#:select => "categories.*, IF(category_translations.name IS NULL, categories.name, category_translations.name) sort_name",
# For sqlite3
:select => "categories.*, (CASE WHEN category_translations.name IS NULL THEN categories.name ELSE category_translations.name END) sort_name",
:joins => ActiveRecord::Base.sanitize_sql_array([
"LEFT JOIN category_translations on category_translations.category_id = categories.id AND category_translations.locale = ?", locale]),
:order => "sort_name"
}
}
...
end
Category.ordered(some_locale).all # Returns all records, sorted by translated name