Skip to content

Instantly share code, notes, and snippets.

@Carmer
Last active July 11, 2016 14:22
Show Gist options
  • Select an option

  • Save Carmer/84b2679b2bd24d9035110a1f32cd4ac0 to your computer and use it in GitHub Desktop.

Select an option

Save Carmer/84b2679b2bd24d9035110a1f32cd4ac0 to your computer and use it in GitHub Desktop.

ActiveRecord Obstacle Course

Getting Started

  • clone this
  • checkout the activerecord-obstacle-course
  • bundle

Now open the project. This is a Rails project, but you don't need to know rails to do these exercises. Navigate to the /spec directory. Within the /models directory you will see a file named activerecord_obstacle_course_spec.rb. Open it and read through the tests. If you want to know more about RSpec you can find it here. With RSpec, :let declares a variable but not until it is called in the code. The :let! declares the variable right there on that line of code.

How to:

Tests in RSpec start with it. Between it do end you will see something like this:

  # ----------------------- Using Ruby -------------------------
    orders_of_500 = Order.all.select { |order| order.amount == 500 }
    orders_of_200 = Order.all.select { |order| order.amount == 200 }
    # ------------------------------------------------------------

    # ------------------ Using ActiveRecord ----------------------
    # Solution goes here
    # ------------------------------------------------------------

    # Expectation
    expect(orders_of_500.count).to eq(1)
    expect(orders_of_200.count).to eq(1)

The first section of code is the implementation in Ruby The last section of code, under the #Expectation are the expectations, or assertions about the code above. Our goal is to comment out the Ruby implementation and write an ActiveRecord implementaion that gets the test to pass.

The very last test is commented out. It brings in a special library, Bullet, that listens for inefficient queries. If you get to the last test read about Bullet gem, uncomment out the test and see if you can figure it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment