In MySql:
Foo.order '(convert(word_one, unsigned) +
convert(word_two, unsigned) +
convert(word_three, unsigned))'
In ActiveRecord:
Foo.all.sort_by do |f|
%w[one two three].sum { |n| f.send("word_#{n}").to_i }
end
Or if these fields are inside Foo:
class Foo < ActiveRecord::Base
def self.sorted_by_sum_of_words
order '(convert(word_one, unsigned) +
convert(word_two, unsigned) +
convert(word_three, unsigned))'
end
end
Then in the controller:
class FooController < ApplicationController
def my_action
@foos = Foo.sorted_by_sum_of_words
end
end
The conversions are unnecessary if you store numbers in number columns.