stevec
stevec

Reputation: 52308

Strange ordering of ActiveRecord object

I am trying to get the records to order by :id in the view. I have records ordered by :id in the controller, like so

@bands = Band.where(available: true).order(:id)

Everything appears normal when the app starts, but as records are updated, the ordering behaviour goes wonky. Most often, recently edited records move to the very end, but not always. This shouldn't happen since the records are ordered by :id which shouldn't be changing

For context, in the view is something like

<% @bands.each do |b| %>
<%= b.name %>
<% end %>

Also note, in the rails console, both of these return the correctly ordered results (which makes this problem even stranger):

ActiveRecord::Base.connection.execute("SELECT * FROM bands WHERE available = 'true' ORDER BY id")

and

Band.where(available: true).order(:id)

Also note, when I load the index view, and observe the rails server, I can see that the results are not in the correct order.

I can also see the sql query that has been executed, and it ignores the order part, it simply doesn't have any mention of order in the query

Upvotes: 0

Views: 57

Answers (1)

Fred
Fred

Reputation: 344

Actually, I don't think it is possible. But, try this:

@bands = Band.where(available: true).order(:id).to_a

Upvotes: 1

Related Questions