0
0
Ruby on Railsframework~10 mins

Scopes for reusable queries in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Scopes for reusable queries
Define Scope Method
Call Scope on Model
Scope Returns Relation
Chain or Execute Query
Get Filtered Results
Scopes are methods defined in a model that return filtered queries. You call them to get reusable, chainable query parts.
Execution Sample
Ruby on Rails
class Product < ApplicationRecord
  scope :available, -> { where(available: true) }
end

Product.available
Defines a scope named 'available' that filters products where available is true, then calls it.
Execution Table
StepActionEvaluationResult
1Define scope :availablescope :available, -> { where(available: true) }Scope method added to Product
2Call Product.availableProduct.availableReturns ActiveRecord::Relation with where(available: true)
3Execute queryProduct.available.to_aArray of products with available = true
4Chain scopes (if any)Product.available.other_scopeCombined query with both filters
5EndNo more callsQuery ready for use or execution
💡 Scopes return relations until executed; execution fetches filtered records.
Variable Tracker
VariableStartAfter 1After 2Final
Product.availableundefinedActiveRecord::Relation (where available: true)Same relationArray of filtered products after execution
Key Moments - 2 Insights
Why does calling Product.available not immediately fetch records?
Because scopes return a relation object that builds the query but delays execution until needed, as shown in execution_table step 2 and 3.
Can scopes be chained together?
Yes, scopes return relations that can be chained to combine filters, as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does Product.available return at step 2?
AAn array of products
BAn ActiveRecord::Relation with a where clause
CA boolean value
DA SQL string
💡 Hint
Check the 'Evaluation' and 'Result' columns at step 2 in the execution_table.
At which step does the query actually fetch records from the database?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for when the relation is converted to an array in the execution_table.
If you chain another scope after Product.available, what happens to the query?
AIt combines filters into one query
BIt executes the query immediately
CIt replaces the previous scope
DIt causes an error
💡 Hint
See step 4 in the execution_table about chaining scopes.
Concept Snapshot
Scopes are reusable query methods in Rails models.
Define with scope :name, -> { query }.
Calling a scope returns a relation, not records.
Scopes can be chained to combine filters.
Query runs when you fetch records (e.g., to_a, each).
Full Transcript
In Rails, scopes let you write reusable query parts inside your model. You define a scope with a name and a lambda that returns a filtered query. When you call the scope, it returns an ActiveRecord relation object, which builds the query but does not run it yet. You can chain multiple scopes to combine filters. The actual database query runs only when you fetch records, like converting to an array or iterating. This makes your code cleaner and queries reusable.