0
0
Ruby on Railsframework~20 mins

Scopes for reusable queries in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Scope Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this scope return?
Given the following Rails model scope, what records will User.active return?
Ruby on Rails
class User < ApplicationRecord
  scope :active, -> { where(active: true) }
end

users = User.active
AAll users regardless of their active status
BAll users with active set to false
CAn error because the scope is not defined correctly
DAll users with the attribute active set to true
Attempts:
2 left
💡 Hint
Think about what the where clause filters.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this scope definition
Which option shows the correct way to define a scope that returns users created in the last 7 days?
Ruby on Rails
class User < ApplicationRecord
  scope :recent, -> { where('created_at >= ?', 7.days.ago) }
end
Ascope :recent, -> { where('created_at >= ?', 7.days.ago) }
Bscope :recent, -> { where(created_at >= 7.days.ago) }
Cscope :recent, -> { where('created_at >= 7.days.ago') }
Dscope :recent, -> { where(created_at: 7.days.ago) }
Attempts:
2 left
💡 Hint
Check how to pass dynamic values in SQL conditions.
state_output
advanced
2:00remaining
What is the output count of this chained scope?
Given these scopes, what is the count of User.active.recent.count if there are 5 active users, 3 users created in last 7 days, and 2 users who are both active and recent?
Ruby on Rails
class User < ApplicationRecord
  scope :active, -> { where(active: true) }
  scope :recent, -> { where('created_at >= ?', 7.days.ago) }
end

count = User.active.recent.count
A8
B2
C5
D3
Attempts:
2 left
💡 Hint
Chaining scopes combines their conditions with AND logic.
🔧 Debug
advanced
2:00remaining
Why does this scope raise an error?
This scope is intended to return users with a minimum age, but it raises an error. Why?
Ruby on Rails
class User < ApplicationRecord
  scope :min_age, ->(age) { where('age >= age') }
end

User.min_age(18)
AThe scope should use a hash instead of a string condition
BThe scope is missing a block parameter
CThe SQL condition uses a string 'age >= age' instead of a placeholder for the parameter
DThe scope is missing a call to .to_i on age
Attempts:
2 left
💡 Hint
Look at how the parameter is used inside the where clause.
🧠 Conceptual
expert
2:00remaining
How do scopes improve code reuse and readability in Rails?
Which option best explains the main benefit of using scopes for reusable queries in Rails?
AScopes allow defining common query logic once and reuse it easily, making code cleaner and easier to maintain
BScopes automatically cache query results to improve performance without extra code
CScopes replace the need for database indexes by optimizing queries internally
DScopes force all queries to be eager loaded to avoid N+1 problems
Attempts:
2 left
💡 Hint
Think about how scopes help avoid repeating the same query code.