Skip to content

Instantly share code, notes, and snippets.

@ihollander
Last active August 14, 2020 10:09
Show Gist options
  • Save ihollander/6836776979cba603261f557ed334f799 to your computer and use it in GitHub Desktop.
Save ihollander/6836776979cba603261f557ed334f799 to your computer and use it in GitHub Desktop.
Active Record Pairing Exercise Instructions

Active Record Pairing Assignment

Today, you'll be working on building out a many-to-many domain using Active Record to practice creating associations between classes, AR-style.

Your mission: take one existing OO Ruby Practice lab, and refactor the code to use Active Record.

You're welcome to work on any old lab you like (so long as it has a many-to-many relationship). Here are some good labs to try:

Setup

  1. Fork & Clone this Active Record template repository: https://github.com/ihollander/mod-1-active-record-template
  2. Open it in your text editor
  3. Copy the README.md file from your practice lab of choice into the AR template repository
  4. Try to solve as many deliverables as you can using Active Record!

NOTE Some of the methods from the deliverables might not map exactly to Active Record methods. For example, classes that inherit from Active Record always initialize with one argument only - a hash. So don't worry if there are minor differences between your implementation and the way the deliverables are phrased - just use this as an opportunity to get familiar with Active Record and see the similarities & differences between our old OO code.

Focuses

  • Creating migrations
  • Connecting classes with Active Record
  • Creating seed data
  • Creating associations using belongs_to, has_many, and has_many through
  • Active Record query methods

Thinking in Active Record

Remember, now that we're using Active Record, the setup for our classes will be different. For example, imagine we're working on these deliverables for a Dog class:

  • Dog#initialize(name, breed, good_dog)
    • should initialize with a name (string), a breed (string), and whether or not this dog is a good dog (boolean)
  • Dog#name
    • should return the name of the dog.
  • Dog#breed
    • should return the breed of the dog.
  • Dog#good_dog
    • should return the dog's good dog status.
  • Dog.all
    • Returns an array of all Dog instances

This is how we might create a Dog class with OO Ruby:

class Dog
  attr_accessor :name, :breed, :good_dog

  @@all = []

  def initialize(name, breed, good_dog)
    @name = name
    @breed = breed
    @good_dog = good_dog

    @@all << self
  end

  def self.all
    @@all
  end

end

But Active Record, we could do something like this instead:

# create a migration with this code:
class CreateDogs < ActiveRecord::Migration[5.2]
  def change
    create_table :dogs do |t|
      t.string :name
      t.string :breed
      t.boolean :good_dog
    end
  end
end

# Create a class and inherit from Active Record
class Dog < ActiveRecord::Base
  # the #initialize method, .all method, and attr_accessor methods are already taken care of by Active Record, so we don't need to add any code here for those methods!
end

Active Record Steps

  1. Install gems: bundle install (just do this once when you first open the project)

  2. Create a migration: rake db:create_migration NAME=create_games

  3. Write the migration:

create_table :games do |t|
  t.string :title
  t.string :genre
  t.integer :price

  t.timestamps # created_at, updated_at
end
  1. Run the migration: rake db:migrate

  2. Check your migrations: rake db:migrate:status (also look at your schema.rb file)

  3. Create your Ruby class and inherit from ActiveRecord:

class Game < ActiveRecord::Base

end
  1. Repeat 1-5 for all your classes

  2. Create seed data in db/seeds.rb and run rake db:seed

  3. Test your code in rake console

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