- Published
- Author
- Syed SibtainSystem Analyst
In Rails,
When we define a
Example:
This ensures a consistent ordering across the app without needing to manually add
It's especially useful when displaying comments, messages, tasks, or anything that should appear in the order they were created.
Caution:
In that case, we would need to call
#rubyonrails
default_scope is a way to automatically apply a query condition to every query for a model.When we define a
default_scope, it always gets added unless we manually remove it.Example:
Ruby
default_scope { order(created_at: :asc) }This ensures a consistent ordering across the app without needing to manually add
.order(created_at: :asc) every time.It's especially useful when displaying comments, messages, tasks, or anything that should appear in the order they were created.
Caution:
default_scope can sometimes be annoying if we want a different order temporarily.In that case, we would need to call
.unscope(:order) to remove it manually.#rubyonrails