Posted about 6 years ago. Visible to the public.

ActiveRecord scopes [4d]


  • Understand what scopes are and how they differ from arrays
  • Understand how a purely object-oriented approach of dealing with data doesn't scale.
  • Get into a habit of off-loading list-related calculations (filtering, mapping) to the database
  • Learn about the N+1 query problem Archive and eager loading
  • Get into a habit of explicitly resolving scopes to arrays by calling to_a (don't let it happen by accident)
  • Understand the difference between "named scopes" and ad-hoc scopes using where
  • Understand how the scope creation syntax changed between Rails 2.3 and Rails 3+



Search for MovieDB

In MovieDB, adapt the existing implementation of the method to return a scope of movies matching the given query format:

  • The query is a list of space-separated words that all need to appear in the movie title in order for the movie to match (e. g. event horizon)

  • A word can be prefixed by a minus character to exclude that word (e.g. event -horizon).

  • If you haven't already considered this, make sure that the returned value is not an array but a scope that can be chained further. E.g. I should be able to use it like this:

    Copy'event -main').search('horizon').where('year > 1995').order(:year).to_a
  • Adapt both the Cucumber scenario and RSpec examples for the search functionality, using the guidelines from the "Testing" chapter in Growing Rails Applications in Practice Archive .

    • RSpec examples should cover all edge cases
    • Cucumber scenario should only cover one "happy path" to show successful integration

Open budgets in Project Hero (makandra only)

In Project Hero (a Rails 3.2 project), build a view that lists all projects that have open (unclosed) budgets with unused amounts:

+----------------------------+-----------------------------------+ | Project | Unused amount in all open budgets | +----------------------------+-----------------------------------+ | Foo | 200 € | | Bar | 150.000 € | +----------------------------+-----------------------------------+
  • First, build it naively by iterating over all projects, iterating over the budgets of each project, etc.
  • Ask someone for a dump of the makandra stage and load it, tail -f log/development.log and observe the performance of your view. How many MySQL queries does it trigger? How many ActiveRecord objects are instantiated?
  • Now build a version of your view that does the same number of queries regardless of how many projects, budgets, activities, etc. you have. It should also not instantiate thousands of ActiveRecord objects, regardless of the database size.
  • When you're done: Can you move your code to a BudgetReport class with a nice API?
  • Note: Please don't use any existing classes that solve a similar problem in PH, such as BudgetCalculator or Analysis::Budgets.

Owner of this card:

Henning Koch
Last edit:
6 months ago
by Klaus Weidinger
Posted by Henning Koch to makandra Curriculum
This website uses short-lived cookies to improve usability.
Accept or learn more