Rails: Default sort order for a rails model? - Stack Overflow

Ask questions Research chat →

https://stackoverflow.com/questions/3393687/rails-default-sort-order-for-a-rails-model · scraped

rails

Attachments

Scraped Content

— 230 words · 2026-02-14 17:41:12 UTC ·

Excerpt

![](https://prod-files-secure.s3.us-west-2.amazonaws.com/871f1661-80b8-4d0c-ac3b-2adfc6ff4c66/81cb2210-0367-4319-bd6d-37c619341959/apple-touch-icon2.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB466VUDP3M25%2F20260214%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20260214T174112Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEEEaCXVzLXdlc3QtMiJIMEYCIQDyqDnXjKM7Xdr2Jpc8mn8NO15BPcqnEt7LiEqC07DVGQIhANdOzjbBHL8OVSsVStTLoE5KX3TgXZ27MDKrbN21OIW4Kv8DCAoQABoMNjM3NDIzMTgzODA1IgyWuCoUrXs4hgzmk20q3AM2TqcgN5BgDjjyWhBRWa14y80%2FdFGAmUJJB%2FW1t7s802iNXXB9pqjFa%2BaC1RDgf22zFXWfA6p%2BzzR%2FktrHpIEJ86FgnipjZaXomXNwWSN4DgMLor2mRUvGB7mLAXRtjezFDd%2FgCUWEa%2FcfjhwrR6XElnqqwo5xDGAMAaCPMV7%2BaOIPr4qC13%2FW62fYXAkDvi%2BWzfEtp8uo%2FZGpJ%2FV2kHpIkrUw8GKmIrcvyxjody9DPIXlhwzjCAKnuP3R6IjTgLjkGKpkoKGhlzPUmgye2y1wjOeA8FZkGBwhOxX7dTy8Hgmubu1%2F3lsxmQxNW25YB5hoT4p4dws%2FKtZJGa0BiftO0AlN9nq3bYRTlM%2BOVTOnPZA2AEfF0jEbMoa0frx3RXGYZnT1pDlDsDNC05xwgnwf531h45%2F
![](https://prod-files-secure.s3.us-west-2.amazonaws.com/871f1661-80b8-4d0c-ac3b-2adfc6ff4c66/81cb2210-0367-4319-bd6d-37c619341959/apple-touch-icon2.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB466VUDP3M25%2F20260214%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20260214T174112Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEEEaCXVzLXdlc3QtMiJIMEYCIQDyqDnXjKM7Xdr2Jpc8mn8NO15BPcqnEt7LiEqC07DVGQIhANdOzjbBHL8OVSsVStTLoE5KX3TgXZ27MDKrbN21OIW4Kv8DCAoQABoMNjM3NDIzMTgzODA1IgyWuCoUrXs4hgzmk20q3AM2TqcgN5BgDjjyWhBRWa14y80%2FdFGAmUJJB%2FW1t7s802iNXXB9pqjFa%2BaC1RDgf22zFXWfA6p%2BzzR%2FktrHpIEJ86FgnipjZaXomXNwWSN4DgMLor2mRUvGB7mLAXRtjezFDd%2FgCUWEa%2FcfjhwrR6XElnqqwo5xDGAMAaCPMV7%2BaOIPr4qC13%2FW62fYXAkDvi%2BWzfEtp8uo%2FZGpJ%2FV2kHpIkrUw8GKmIrcvyxjody9DPIXlhwzjCAKnuP3R6IjTgLjkGKpkoKGhlzPUmgye2y1wjOeA8FZkGBwhOxX7dTy8Hgmubu1%2F3lsxmQxNW25YB5hoT4p4dws%2FKtZJGa0BiftO0AlN9nq3bYRTlM%2BOVTOnPZA2AEfF0jEbMoa0frx3RXGYZnT1pDlDsDNC05xwgnwf531h45%2Fowku8RvZaMbFssN0BgwUaIhijz%2FTFfkQH4Uhp95z%2FAfVXvzCGFOpuuQ%2FYTLK7tkeJhuj9FCI04GZFi3zM8Bnx%2BWYvc4d4TAiE7L8yv5GnXjvzmdzd%2FoTcRLI52tCg6vlE8CdFrin1OiKDnJC99fdoL3DNLAL7IPHEJ2wRzOLT663d4ChfhXFBAOMybvNvnVka7HE7%2FpYBKKjWfFRcXk4IoVRd9AwYWTCk0sLMBjqkAafLObLyHdvQ0jNTl4sfjH0%2Fa9aqjCXT96qQOvigOC0wIfA3dAHyBREinnOpyo2dqMiE%2BjWBRn0BldF7m3eIPgCi7TsK0HhSovR0hsqkL%2BxNQWMau3MO6cpzcFxze9Jds4l00G1XKwfDe0ifpThxxil6jQO5UN3hgvbjnDoTt2GnKf3BaU8ZkrtnmFiVtILwKEpMppvZlYd3%2BS4Pn1OGqoJm3zm5&X-Amz-Signature=3d03ebc57a0844e0156fc33cedbde249c2c57c41be870ba22a550392865434cf&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject) ## default_scope This works for Rails 4+: ```plain text class Book < ActiveRecord::Base default_scope { order(created_at: :desc) } end ``` For Rails 2.3, 3, you need this instead: ```plain text default_scope order('created_at DESC') ``` For Rails 2.x: ```plain text default_scope :order => 'created_at DESC' ``` Where created_at is the field you want the default sorting to be done on. Note: ASC is the code to use for Ascending and DESC is for descending (desc, NOT dsc !). ## scope Once you're used to that you can also use scope: ```plain text class Book < ActiveRecord::Base scope :confirmed, :conditions => { :confirmed => true } scope :published, :conditions => { :published => true } end ``` For Rails 2 you need named_scope. :published scope gives you Book.published instead of Book.find(:published => true). Since Rails 3 you can 'chain' those methods together by concatenating them with periods between them, so with the above scopes you can now use Book.published.confirmed. With this method, the query is not actually executed until actual results are needed (lazy evaluation), so 7 scopes could be chained together but only resulting in 1 actual database query, to avoid performance problems from executing 7 separate queries. You can use a passed in parameter such as a date or a user_id (something that will change at run-time and so will need that 'lazy evaluation', with a lambda, like this:

Visibility

Visible to everyone

Reading Status

Related Bookmarks

My Note


Saved!

Annotations

Export as Markdown
+ Annotate selection

Add Annotation