Intro to Rails
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
- 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
Best Song App
An application that lists artists and allows users to vote for their favorite songs by artist, and suggest new songs to vote on.
- 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
More info on writing good user stories...
- 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.
Best Song App User Stories
- Data Associations
What do we need?
Let's Build an App!
$ rails generate model Artist full_name:string
$ rails generate model Song title:string artist:belongs_to
$ rails generate model Vote song:belongs_to
Generating models also generated database migrations for us. Let's check them out and make sure they list the attributes we want.
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.
create_table :artists do |t|
Ok, now that it looks good, run the migrations.
$ rake db:migrate
Migrations in more detail...
- 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.
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
add_column :songs, :optimal_volume, :string
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.
The second part of building data associations is in the models.
class Artist < ActiveRecord::Base
class Song < ActiveRecord::Base
class Vote < ActiveRecord::Base
Rails supports six types of associations:
- has_many :through
- has_one :through
Note: the last two are not used very often.
Associations in more detail...
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.
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.
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.
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'.
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.
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.
- 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.