Giter Site home page Giter Site logo

sinatra-activerecord-associations-atlanta-web-021720's Introduction

ActiveRecord Associations in Sinatra

Objectives

  1. Understand primary and foreign key.
  2. Understand the has many/ belongs to relationship.

Introduction

Previously, we learned about foreign and primary keys in SQL and how they relate tables to one another. In this lesson, we're going to learn how to do this using ActiveRecord. This lab has pre-written code that you can follow along with - feel free to fork and clone the repo locally.

Primary Keys

The end goal of this readme is to create a relationship in our app that mimics the real life cat-owner relationship: owners can have many cats and cats belong to an owner. Let's assume we have two tables in our database: cats and owners, which we created from the command line using rake.

Review: Creating a table with ActiveRecord

First, we create a cats table from the command line: rake db:create_migration NAME="create_cats"

This will give us an empty migration in our db/migrate/ folder. Now lets give our cats table attributes: name, age and breed. This will go into our change method.

class CreateCats < ActiveRecord::Migration
  def change
    create_table :cats do |t|
      t.string :name
      t.integer :age
      t.string :breed
    end
  end
end

Review: Primary Keys

A primary key uniquely identifies each record in a table. It must be unique and cannot have NULL values. Luckily, ActiveRecord will create the primary key for us and will also auto-increment it every time we save a new row in our table.

Go ahead and use Tux to create three instances of the Cat class:

Cat.create(:name => "Maru", :age => 3, :breed => "Scottish Fold")
Cat.create(:name => "Hannah", :age => 2, :breed => "Tabby")
Cat.create(:name => "Patches", :age => 2, :breed => "Calico")

And two instances of our Owner class:

Owner.create(:name => "Sophie")
Owner.create(:name => "Ann")

Our cats table looks like this:

id name age breed
1 Maru 3 Scottish Fold
2 Hannah 2 Tabby
3 Patches 2 Calico

Our owners table looks like this:

id name
1 Sophie
2 Ann

Now, we need to tell our tables how to relate to each other. This is where we'll use a foreign key.

Using Foreign Keys

A foreign key points to a primary key in another table. In ActiveRecord we will use the tablename_id convention. To add the foreign key to our cats table, we will create another migration.

The foreign key always sits on the table of the object that belongs to. In this case, because cats belong to an owner, the owner_id becomes a column in the cats table.

class AddColumnToCats < ActiveRecord::Migration
  def change
    add_column :cats, :owner_id, :integer
  end
end

Our cats table should look like this:

id name age breed owner_id
1 Maru 3 Scottish Fold 1
2 Hannah 2 Tabby 2
3 Patches 2 Calico 1

We now know what our table should look like. However, we haven't told our application how to relate the models to each other.

belongs_to and has_many

Before we write our association let's think about our table structure: A cat belongs to an owner, and an owner can have many cats.

This translates into ruby like this:

class Cat
  belongs_to :owner
end
class Owner
  has_many :cats
end

Whenever we use a has_many we also have to use the belongs_to (and vice-versa) in the other model. Keep in mind: The model with the belongs_to association also has the foreign key.

Creating objects

After setting our associations, we can create a cat and an owner and save them to our database. Try using Tux to play around. Create objects, view them, edit them, delete them!

sophie = Owner.create(name: "Sophie")
maru = Cat.new(name: "Maru", age: 3, breed: "Scottish Fold")
maru.owner = sophie
maru.save

We used the .create method to instantiate and save the owner to our database. To instantiate the cat object we used the .new method, after that we set "Maru's" owner to the owner we created. Because the .new method did not save the cat object to our database the last line will persist the cat object to our database.

The has_many/belongs_to relationship is the most used association, but there are others as well. You can read more about ActiveRecord Associations here.

sinatra-activerecord-associations-atlanta-web-021720's People

Contributors

dfenjves avatar rrcobb avatar annjohn avatar dependabot[bot] avatar lizbur10 avatar danielseehausen avatar drakeltheryuujin avatar brennenawana avatar cernanb avatar franknowinski avatar curiositypaths avatar jmburges avatar morgvanny avatar rishter avatar victhevenot avatar

Watchers

 avatar  avatar  avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar  avatar  avatar Ben Oren avatar Matt avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman avatar  avatar Lisa Jiang avatar Vicki Aubin avatar Maxwell Benton avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.