1

In my model, I have this method which takes the last_name and first_name columns for an object and concatenates them into a string.

def name
  last_name + " " + first_name
end

I want to define a scope that can sort my objects by that method. How would one go about doing that, using my method? I don't want to define some scope that first sorts by last_name, and then by first_name in the scope (if that's even possible). My understanding that you can only scope on actual columns in the rails framework? Is that incorrect?

Here's what I wrote, but obviously neither works, as there is no name field in my AdminUser table. Not sure why the second one doesn't work, but I'm guessing that the :name_field wouldn't work, as it's not actually in the model/database as a column.

scope :sorted, lambda { order("name ASC")}
scope :sorted, lambda { :name_field => name, order("name_field ASC")}
sawa
  • 165,429
  • 45
  • 277
  • 381
PianoFingers
  • 117
  • 2
  • 6

1 Answers1

2

Unfortunately it is not possible to do this directly from Ruby/Rails to SQL. You could achieve what you want in two ways

You can load all the users into memory and sort them in Ruby

User.all.to_a.sort_by(&:name)

Or you can define an order in SQL as such

ORDER BY CONCAT(users.last_name, ' ', users.first_name) ASC;

In Rails, you'd have to do the following

scope :sorted, -> { 
  order("CONCAT(users.last_name, ' ', users.first_name) ASC") 
}

Do note that this may not be portable between DBs.

Leo Correa
  • 19,131
  • 2
  • 53
  • 71