active record data modeling

17
Data Modeling WITH ACTIVE RECORD IN RAILS

Upload: hannah-howard

Post on 04-Jul-2015

98 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Active Record Data Modeling

Data Modeling WITH ACTIVE RECORD IN RAILS

Page 2: Active Record Data Modeling

About Me:

Hannah HowardTwitter: @techgirlwonderEmail: [email protected]

I run my own computer services business called Tech Girl Wonder. And I’m hiring.

I do ruby programming with Logical Reality Design, a Rails development shop. Hire us!

Page 3: Active Record Data Modeling

• This is my take on the subject, not the one true way.

• For the smarty pants in the room, I’m skipping over a lot. Possibly even saying things that aren’t always true.

• There will be time for questions.

• This is not a lecture, only the first 15-30 minutes are.

A Few CAVEATS

Page 4: Active Record Data Modeling

Why This Talk?

• I think I’ve noticed some patterns in where new people get stuck learning Rails

• I think it has to do with the parts of Rails that are closer to traditional computer programming and very different from HTML/CSS

• Specifically, I think people get stuck on the ‘M’ in the MVC, i.e ActiveRecord and its connection to an SQL database.

Page 5: Active Record Data Modeling

STATIC WebSites VS

Web ApplicationsWeb ApplicationsWeb Applications

Page 6: Active Record Data Modeling

Static Web Sites

Server

Give me a page!

Here’s a page!

Client

Static web page: is delivered to the user exactly as stored.

Page 7: Active Record Data Modeling

Web APPLICATION

Information

Presented To The User

Data Retrieved From The

Model

Model Of Data

Represented by App

List of changes sent to

the model

Interface For

Receiving Input

ServerClient View Controller

Gives Input

Sees Results

Page 8: Active Record Data Modeling

WHAT IS THE MODEL?

• A model of data represented by the application

• Rails stores its data in a relational database. (usually)

• You access data in relational databases with SQL queries

• SQL queries are complex and hard to write

• ActiveRecord is there to make that easier and “better”

Page 9: Active Record Data Modeling

What is A RELATIONAL DATABASE?

• A series of data tables (like Excel spreadsheets) that are connected to each other through relationships

• Hint: Spend some time with Filemaker or Access

Page 10: Active Record Data Modeling

RelationAL Database Example

Id Weight Stale

1 6oz TRUE

2 8oz FALSE

3 5oz FALSE

Id Name Country

1 Swiss Switzerland

2 American U.S.

3 Harvarty Denmark

Id Name Address

1 Cheesetopia 123 Fake St.

2 I Love Cheese 8675 Threeonine Ave.

3 Bob’s Cheeses 1 Infinite Loop

Cheese Types

Cheeses

Cheese Shops

Id Weight Stale TypeID ShopID

1 6oz TRUE 1 2

2 8oz FALSE 3 3

3 5oz FALSE 2 1

Have Many Have

Many

Have Many

Have Many

Belongs To

Belongs To

TypeID ShopID

3 2

1 2

2 1

1 1

3 3

2 3

Page 11: Active Record Data Modeling

SQL: Standard Query Lanaguage

SELECT * FROM CHEESES INNER JOIN CHEESE_TYPES ON cheeses.cheese_type_id = cheese_types.id WHERE cheese_types.name IN [“SWISS”, “HARVARTI”] WHERE cheeses.stale = FALSE

Page 12: Active Record Data Modeling

AcTIVERECORDCheese Typesclass CheeseType < ActiveRecord::Base has_many :cheeses has_and_belongs_to_many :cheese_stores attr_accessible :name, :countryend

Cheesesclass Cheese < ActiveRecord::Base belongs_to :cheese_type belongs_to :cheese_store attr_accessible :weight, :staleend

Cheese Storesclass CheeseStore < ActiveRecord::Base has_many :cheeses has_and_belongs_to_many :cheese_types attr_accessible :name, :addressend

Page 13: Active Record Data Modeling

WoRKING WITH ACTIVE RECORD

cheese_type.country = “France”

cheese.cheese_store

cheese_store.cheese_types

Cheese.where(:weight => 4.0..10.0)

Cheese.joins(:cheese_types).where(cheese_types: {name: [“SWISS”, “HAVARTI”]}).where(:stale => false)

Page 14: Active Record Data Modeling

The KEY PIECE OF GLUE: Migrations

rails generate model cheeses weight:decimal stale:boolean cheese_type_id:integer cheese_store_id:integer

class CreateCheeses < ActiveRecord::Migration def change create_table :cheeses do |t| t.decimal :weight t.boolean :stale t.integer :cheese_type_id t.integer :cheese_store_id

t.timestamps end endend

Oh wait, so that’s what rake db:migrate does!

Page 15: Active Record Data Modeling

Are You READY TO START PROGRAMMING?

Page 16: Active Record Data Modeling

rb-tunes

Our Project:

iTunes

Page 17: Active Record Data Modeling

Lets Start Making It!

Tracks

Albums

Artists

Playlists