evanvee
evanvee

Reputation: 315

Where Clause - Ruby on Rails

I am new to Ruby on Rails and am having trouble with a simple where with a model.

When I try to do Test #1 the results are out of order. New items get pushed to the bottom no matter what.

    def index
    @user = User.where(:status => false).order(last_name: :desc).all
    end

If I enter this into rails console it doesn't work also but if I remove the all it works perfectly in rails console but doesn't work in the UsersController.

What is the proper way to do a where clause with an order? Thanks for your help!

UPDATE:

I have updated the code to the following but the results are still out of order:

    def index
        @user = User.where(status: false).order('last_name DESC')
    end

Upvotes: 1

Views: 1029

Answers (2)

cowgill
cowgill

Reputation: 166

Rails 4

def index
    @user = User.where(status: false).order('last_name DESC')
end

Upvotes: 1

xdazz
xdazz

Reputation: 160923

You should use order('last_name DESC') instead of order(last_name: :desc).

order(last_name: :desc) will produce sql like (That's why your order doesn't work):

ORDER BY '---\\n:last_name: :desc\\n'

order('last_name DESC') will produce sql right:

ORDER BY last_name DESC

Upvotes: 1

Related Questions