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:
- Dog Walker: https://github.com/ihollander/mod-1-practice-challenge-dog-walker
- Blood Oaths
- Flatiron Mifflin
- Silicon Valley
- Fork & Clone this Active Record template repository: https://github.com/ihollander/mod-1-active-record-template
- Open it in your text editor
- Copy the README.md file from your practice lab of choice into the AR template repository
- 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.
- Creating migrations
- Connecting classes with Active Record
- Creating seed data
- Creating associations using
belongs_to
,has_many
, andhas_many through
- Active Record query methods
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
-
Install gems:
bundle install
(just do this once when you first open the project) -
Create a migration:
rake db:create_migration NAME=create_games
-
Write the migration:
create_table :games do |t|
t.string :title
t.string :genre
t.integer :price
t.timestamps # created_at, updated_at
end
-
Run the migration:
rake db:migrate
-
Check your migrations:
rake db:migrate:status
(also look at yourschema.rb
file) -
Create your Ruby class and inherit from ActiveRecord:
class Game < ActiveRecord::Base
end
-
Repeat 1-5 for all your classes
-
Create seed data in
db/seeds.rb
and runrake db:seed
-
Test your code in
rake console