0
0
Ruby on Railsframework~10 mins

Scopes for reusable queries in Ruby on Rails - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a scope named active that returns only active users.

Ruby on Rails
scope :active, -> { where(status: [1]) }
Drag options to blanks, or click blank then click option'
Aactive
B:active
C"active"
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a symbol instead of a string for the status value.
Leaving out quotes causing a NameError.
2fill in blank
medium

Complete the code to define a scope named recent that returns users created in the last 7 days.

Ruby on Rails
scope :recent, -> { where('created_at >= ?', [1]) }
Drag options to blanks, or click blank then click option'
A7.days.ago
BTime.current + 7.days
CDate.today - 7
DTime.now - 7.days
Attempts:
3 left
💡 Hint
Common Mistakes
Using Time.now - 7.days which works but is less idiomatic.
Using a future date like Time.current + 7.days.
3fill in blank
hard

Fix the error in the scope that filters users by minimum age.

Ruby on Rails
scope :min_age, ->(age) { where('age >= [1]', age) }
Drag options to blanks, or click blank then click option'
A:age
B?
C$1
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name directly inside the SQL string.
Using symbols or numbered placeholders which are not supported here.
4fill in blank
hard

Fill both blanks to define a scope by_role that filters users by a given role.

Ruby on Rails
scope :by_role, ->(role) { where([1]: [2]) }
Drag options to blanks, or click blank then click option'
A:role
Brole
C:status
Dstatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong key like :status instead of :role.
Using the symbol :role as the value instead of the variable role.
5fill in blank
hard

Fill all three blanks to define a scope search_by_name that finds users with names containing a search term, case-insensitive.

Ruby on Rails
scope :search_by_name, ->(term) { where("LOWER(name) [1] LOWER([2])", [3]) }
Drag options to blanks, or click blank then click option'
ALIKE
Bterm
C'%' || ? || '%'
DILIKE
Attempts:
3 left
💡 Hint
Common Mistakes
Using ILIKE which is PostgreSQL specific and not always supported.
Not using LOWER to make the search case-insensitive.
Passing the term directly without wildcards.