Skip to content

Commit

Permalink
order users by profile.first_name
Browse files Browse the repository at this point in the history
  • Loading branch information
nstrelow committed Jan 17, 2017
1 parent 2387142 commit 53e2abc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Expand Up @@ -4,7 +4,7 @@ class UsersController < ApplicationController

# GET /users
def index
@users = User.all.paginate(:page => params[:page], :per_page => 5)
@users = User.with_profiles.paginate(:page => params[:page], :per_page => 5)
if params[:search]
@users = User.search(params[:search]).paginate(:page => params[:page], :per_page => 5)
end
Expand Down
12 changes: 10 additions & 2 deletions app/models/user.rb
Expand Up @@ -111,7 +111,15 @@ def rejected_applications_count(event)
# @param pattern to search for
# @return [Array<User>] all users with pattern in their name
def self.search(pattern)
joins(:profile).where("profiles.first_name LIKE ? or profiles.last_name LIKE ?",
"%#{pattern}%", "%#{pattern}%")
with_profiles.where("profiles.first_name LIKE ? or profiles.last_name LIKE ?", "%#{pattern}%", "%#{pattern}%")
end

# Provides access to profile information
# and orders users by first, last name and email (if user has no profile)
#
# @return [Array<User>] all users including their profile information
def self.with_profiles()
joins("LEFT JOIN profiles ON users.id = profiles.user_id")
.order('profiles.first_name, profiles.last_name, users.email ASC')
end
end

0 comments on commit 53e2abc

Please sign in to comment.