active record makeover

32
Active Record Makeover Marty Haught @mghaught http://martyhaught.com

Upload: marty-haught

Post on 10-May-2015

1.001 views

Category:

Technology


4 download

DESCRIPTION

15 minute talk given at Red Dirt Ruby Conf in Oklahoma City - May 6th

TRANSCRIPT

Page 1: Active record makeover

Active Record Makeover

Marty Haught@mghaught

http://martyhaught.com

Page 2: Active record makeover

Longmont, Colorado

Boulder Ruby

Page 3: Active record makeover

ActiveRecord feels like

Page 4: Active record makeover

Growing tired of this

Page 5: Active record makeover

Wished I was driving...

Page 6: Active record makeover

...but I can’t

• Existing system

• Project already committed

Page 7: Active record makeover

or doesn’t make sense

• Not the best fit for project

• No significant gain

• rdbms solves the problem

Page 8: Active record makeover

So where’s the love?

• Where’s the new hotness

• Envious of the nosql gems

• Want a better way to model data

Page 9: Active record makeover

Rails 3’s ActiveRecord

Unpimp your auto

Page 10: Active record makeover

Engine’s been rebuilt

• ActiveRelation

• arel gem - relational algebra

• Consistency under the hood

Page 11: Active record makeover

Easier to model with

• Simpler api

• Fully chainable

• Lazy loaded

Page 12: Active record makeover

find all

Rails 2x

Rails 3

Page 13: Active record makeover

first, last, count

Rails 2x

Rails 3

Page 14: Active record makeover

Finder methods

Page 15: Active record makeover

Chainable

Rails 2x

Rails 3

Page 16: Active record makeover

What about scopes?

Rails 2x

Rails 3

Page 17: Active record makeover

Reusing scopes

Page 18: Active record makeover

Merging scopes

Page 19: Active record makeover

Using scoped

Page 20: Active record makeover

Mix and match

Page 21: Active record makeover

Behaves like the model

newcreatecreate!finddestroydestroy_all

deletedelete_allupdateupdate_allexists?

Page 22: Active record makeover

Building with where values

Page 23: Active record makeover

Lazy loaded

• won’t interact with the db until accessed

• such as each, map, etc.

• force load - all, first, last

Page 24: Active record makeover

Plays nice with caching

Page 25: Active record makeover

Modeling with class

Page 26: Active record makeover

to_sql

Page 27: Active record makeover

where_values

Page 28: Active record makeover

Extend the possibilities

• ActiveRecord::QueryMethods opens a door

• invokes to_sql on where_values unless a String

• pass in your own objects that create sql

Page 30: Active record makeover

Got some hotness

• new engine

• concise and readable

• simpler to model

• flexible

• extendable

Page 31: Active record makeover

Go have fun, get dirty

Page 32: Active record makeover

Thank youMarty Haught

@[email protected]

http://martyhaught.com

wagon - http://www.flickr.com/photos/halfy/3410561333vw car - http://www.youtube.com/watch?v=qgEvy60bZYIwhite race car - http://www.flickr.com/photos/33031095@N03/4538638400/yellow sports car - http://www.flickr.com/photos/retinafunk/57699167/red sports car - http://www.flickr.com/photos/freefoto/3844246045/gray sports car - http://www.flickr.com/photos/doramosnoopy/2479741916/blue sports car - http://www.flickr.com/photos/chorwedel/4398697300/racing cars - http://www.flickr.com/photos/oizumi/2582189751/

Image Credits