Intro to Rails

slides: http://cherimarie.github.io/gdi-rails

Welcome!

Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.

Some "rules"

  • We are here for you!
  • Every question is important
  • Help each other
  • Have fun

What we will cover today

  • Homework review
  • Write user stories for new app
  • Plan app architecture
  • Build models
  • Build data associations

Homework Discussion

Questions?

Best Song App

Description

An application that lists artists and allows users to vote for their favorite songs by artist, and suggest new songs to vote on.

User Stories

  • Stories guide development- how will your product be used?
  • Should be for small, testable features
  • Written with input from the stakeholders
  • Formal format is: As a (role) I want (goal) so that (reason).
  • Give them a priority ("required", "nice to have", "v2")
  • Give them an estimated time/size (how hard will this be to do?)
  • During the project, stories may change, be broken into smaller pieces, be re-prioritized or re-estimated

User Story Examples

  • As a user, I can backup my entire hard drive.
  • As a user, I can indicate folders not to backup so that my backup drive isn't filled up with things I don't need saved.
  • Parking passes can be paid via credit cards.
  • Students can only enroll in seminars for which they have prerequisites.
More info on writing good user stories...

Best Song App User Stories

????

App Architecture

  • Models
  • Data Associations
  • Controllers
  • Views


What do we need?

Let's Build an App!

Generating Models


$ rails generate model Artist full_name:string 
$ rails generate model Song title:string artist:belongs_to
$ rails generate model Vote song:belongs_to            
          

Migrations

Generating models also generated database migrations for us. Let's check them out and make sure they list the attributes we want.

Migrations

Let's add 'current hairstyle' attribute to Artist table!

Since the migration hasn't been run yet, we can just edit it to add the new column.


def change
  create_table :artists do |t|
    t.string :name
    t.string :current_hairstyle
    t.timestamps
  end
end
          

Ok, now that it looks good, run the migrations.


$ rake db:migrate
          

Migrations

  • Migrations describe changes that will be made to the database.
  • They use a Ruby DSL so you don't have to write SQL.
  • Each migration modifies the database's schema, which you can see in your db/schema.rb file.
Migrations in more detail...

Migrations

It would be pretty cool if songs has an attribute called 'optimal volume'. Let's create a migration to add that to the table. Run this in the terminal:


$ rails generate migration AddOptimalVolumetoSongs
          

And this will be the migration file it generates, with your additions:


class AddOptimalVolumetoSongs < ActiveRecord::Migration
  def change
    add_column :songs, :optimal_volume, :string
  end
end
          

Sweet. Now run the migration ('rake db:migrate') so the schema is up to date.

Let's Develop It!

Generate a migration to add a column called 'hometown' with a type of 'string' to the Artists table.

Data Associations

The second part of building data associations is in the models.


class Artist < ActiveRecord::Base
  has_many :songs 
end

class Song < ActiveRecord::Base
  belongs_to :artist
  has_many :votes 
end

class Vote < ActiveRecord::Base
  belongs_to :song
end            
          

Data Associations

Rails supports six types of associations:

  • belongs_to
  • has_one
  • has_many
  • has_many :through
  • has_one :through
  • has_and_belongs_to_many

Note: the last two are not used very often.

Associations in more detail...

Data Associations

belongs_to

This sets up either a one-to-one or one-to-many model with another mdoel.

  • If order belongs to customer, each order can be assigned to exactly one customer.
  • In the order table, there will be an entry for 'customer_id: integer'.
  • The order model will say 'belongs_to :customer'. Note that 'customer' is singular!
  • The customer model will need to either 'have one' or 'have many' orders.
  • Data Associations

    has_one

    This sets up a one-to-one connection with a another model.

  • If supplier has one account, each supplier can have exactly one associated account.
  • In the supplier table, there will not be a reference to accounts.
  • The supplier model will say 'has_one :account'. Note that 'account' is singular!
  • The account model will need to 'belong to' supplier.
  • Data Associations

    has_many

    This sets up a one-to-many connection with another model.

  • If customer has many orders, each customer may be associated with zero or more orders.
  • In the customer table, there will not be a reference to orders.
  • The customer model will say 'has_many :orders'. Note that 'orders' is plural!
  • Orders will need to 'belong to' customer.
  • Data Associations

    has_many :through

    This sets up a many-to-many connection to another model, through a third model.

  • A patient can have many physicians through an appointment and a physician can have many patients through an appointment.
  • In the appointments table, there will be an entry for both physician_id and patient_id.
  • The appointment model will say 'belongs_to :physician, belongs_to :patient', and the patient model will say 'has_many :appointments, has_many :physicians, through: :appointments'.
  • Data Associations

    Index?

    A database index is a data structure that improves the speed of data retrieval at the cost of more storage space. An index is a copy of the selected columns, which can be searched very efficiently.

    Summary

    Today, we planned out our application by writing user stories and discussing the architecture. Then, we generated the models that we'll be using, and hooked up the associations properly.

    Questions?

    Homework

    • Do the first three levels of Rails for Zombies
    • Spend some time in the Rails Console with your Best Song App, creating, destroying, and updating database records.