I know that Job.all
returns an array of all jobs.
But, what would be the order ?
Are they ordered by ascending id
?
What Job.first
returns ? The documentation says: "Returns the first resource found."
But, what is the looking order ?
The default order is however the DB decided to return them.
See here for more info.
ActiveRecord Find All not sorting by ID?
If you want them in a specific order, you should do Model.order()
There is no order. You should watch your logs while learning about ActiveRecord to see what SQL is being generated. If there's no ORDER BY clause, there's no order. You may find that you get records back in the order in which they were inserted to the database but that's just coincidental and due to implementation within the database server. SQL results are explicitly unordered unless ORDER BY is present.
As for #first, that is also random without an order clause (at least, it is in rails 3).
You can specify the order quite easily:
MyModel.order(:some_attr) # all records sorted by some_attr
MyModel.order(:some_attr).first # First record in sorted order
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With