Mini John
Mini John

Reputation: 7941

.order() by attributes

Every answer has a :votes_count attribute and i'm trying to order by that count my answers.

I tried

def index
  @answers = Answer.all.order("votes_count desc")
end

but i think i'm missing something here.

Upvotes: 0

Views: 169

Answers (1)

Tyler
Tyler

Reputation: 11509

Answer.order("votes_count desc")
Answer.order(:votes_count).reverse

Either should work.

EDIT

You can do what you described by:

@answers = Answer.order("votes_count desc")
@toggled = @answers.where(accept_toggle: true)
@answers = @toggled + (@answers - @toggled)

This will put all the answers that have accept_toggle == true at the front of the array, followed by all the other answers sorted by votes_count

Upvotes: 1

Related Questions