Recall & Review
beginner Click to reveal answer
What is a scope in Rails?
A scope is a way to define reusable, named queries in a Rails model. It helps keep code clean by grouping common query logic.
beginner Click to reveal answer
How do you define a simple scope in a Rails model?
Use the
scope method with a name and a lambda. Example: scope :active, -> { where(active: true) }beginner Click to reveal answer
Why use scopes instead of repeating query code?
Scopes make code easier to read, maintain, and reuse. They avoid repeating the same query logic in many places.
intermediate Click to reveal answer
Can scopes accept parameters? How?
Yes. You can pass parameters to scopes by using lambdas with arguments. Example:
scope :created_after, ->(date) { where('created_at > ?', date) }intermediate Click to reveal answer
How do scopes chain together in Rails?
Scopes return ActiveRecord relations, so you can chain them like methods. Example:
User.active.created_after(Date.today) runs both queries together.What does a Rails scope return?
✗ Incorrect
Scopes return ActiveRecord relations, allowing further chaining and lazy loading.
How do you define a scope named 'recent' that returns records created in the last 7 days?
✗ Incorrect
The correct syntax uses
scope :name, -> { ... } with a lambda.Which is a benefit of using scopes?
✗ Incorrect
Scopes help reuse query logic and improve code clarity.
Can you chain multiple scopes in Rails?
✗ Incorrect
Scopes return ActiveRecord relations, so chaining is supported.
How do you pass a parameter to a scope?
✗ Incorrect
Scopes can accept parameters by defining lambdas with arguments.
Explain what a scope is in Rails and why it is useful.
Think about how you can avoid repeating the same query code.
You got /4 concepts.
Describe how to define a scope that accepts a parameter and how to use it.
Remember scopes are like methods that return queries.
You got /4 concepts.
