Mini John
Mini John

Reputation: 7941

.order() by attributes with 2 conditions

Every answer has a :votes_count attribute and is ordered by it.

Additionally every answer has :accept_toggle attribute.

Current code:

<div class="">
  <%= render @question.answers.order(:votes_count).reverse %>
</div>

How can i still order answers by :votes_count but as soon an answer has an :accept_toggle it is raised to the top.

Upvotes: 0

Views: 60

Answers (1)

sites
sites

Reputation: 21775

Try this, deleting reverse, this would sort answers first by accept_toggle and then by votes count:

<%= render @question.answers.order(accept_toggle: :desc, votes_count: :desc)%>

If you want to sort them first by number of votes and then by accept_toggle try:

<%= render @question.answers.order(votes_count: :desc, accept_toggle: :desc)%>

Docs.

Upvotes: 1

Related Questions