I have an ActiveRecord model with a field called name
(in the database) and a method called collation_name
(not in the database) that returns a reduced version of the name. The model class resembles this:
class Foo < ActiveRecord::Base
# ... other stuff ...
def collation_name
words = name.downcase.split
if words[0] == 'the'
words.shift
end
words.join(' ')
end
end
My application is used to export the application database to a second database containing a denormalized version of the data, including collation_name
as a real column. I'd like to use collation_name
to order records for display within the application's views. I tried Foo.all(:order=>:collation_name)
but got a 'no such column: collation_name' error.
How do I get a list of Foo
records ordered by collation_name
? Do I have to add collation_name
as a real column in the application database for ActiveRecord to see it when ordering? Should I sort in my application code after ActiveRecord has returned results?