Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default order from a has_many relationship in Rails?

What is the default order from a has_many relationship in Rails?

Thought that in the ActiveRecord:Base the default_scope was order: 'created_at ASC', but I had to specify to get it ordering properly.

I have looked in the docs and in the source code, but I could not find any reference on that!

like image 389
Thiago Diniz Avatar asked Sep 19 '25 23:09

Thiago Diniz


1 Answers

There is no order specified by default, so they will be ordered by how the database returns them. Usually this is by ID or insert order. The created_at column is only there if you specify it in your migrations so it wouldn't make sense for that to be the default order column.

like image 151
Peter Brown Avatar answered Sep 22 '25 12:09

Peter Brown