Total Blog Views: 49
Blog Status: publish
Created By: swazahmad Created at: 07-11-2021
Tags:
Scopes are custom queries written in the model used for retrieving and querying objects.
A scope takes name and lambda
scope :admin, → {where(is_admin: true)}
How to call this scope
User.admin
scope :created between, -> (from, to) {
where(
"created_at > ? AND created_at < ?", from, to
)}
Invoice.created between(test, today)
A default scope gets automatically applied whenever you access the model. Be careful when this is used.
default_scope { where(active: true }
# returns only active users User.all
What is Unscoped in ruby on rails?.
If you want to remove all the previously applied scopes from the model, use unscoped.
scope :active, → {where(active: true)}
# returns all users User.active.unscoped
Scope will always return Activerecord::Relation object.
we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.
© Copyright Shadbox. All Rights Reserved
Rate Blog :
Share on :
Do you have any blog suggestion? please click on the link