find_by_sql() custom columnts trick
Just stumbled on the fact that ActiveRecord automatically creates properties for all column in a SQL query, not just those in the table.
Model.find_by_sql("SELECT *, COUNT(*) AS rank FROM ...").each do |item|
puts "%s (%d)" % [item.name, item.rank]
end
No comments yet, be the first one!
Leave a Reply