active record makeover
DESCRIPTION
15 minute talk given at Red Dirt Ruby Conf in Oklahoma City - May 6thTRANSCRIPT
![Page 1: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/1.jpg)
Active Record Makeover
Marty Haught@mghaught
http://martyhaught.com
![Page 2: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/2.jpg)
Longmont, Colorado
Boulder Ruby
![Page 3: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/3.jpg)
ActiveRecord feels like
![Page 4: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/4.jpg)
Growing tired of this
![Page 5: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/5.jpg)
Wished I was driving...
![Page 6: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/6.jpg)
...but I can’t
• Existing system
• Project already committed
![Page 7: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/7.jpg)
or doesn’t make sense
• Not the best fit for project
• No significant gain
• rdbms solves the problem
![Page 8: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/9.jpg)
Rails 3’s ActiveRecord
Unpimp your auto
![Page 10: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/10.jpg)
Engine’s been rebuilt
• ActiveRelation
• arel gem - relational algebra
• Consistency under the hood
![Page 11: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/11.jpg)
Easier to model with
• Simpler api
• Fully chainable
• Lazy loaded
![Page 12: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/12.jpg)
find all
Rails 2x
Rails 3
![Page 13: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/13.jpg)
first, last, count
Rails 2x
Rails 3
![Page 14: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/14.jpg)
Finder methods
![Page 15: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/15.jpg)
Chainable
Rails 2x
Rails 3
![Page 16: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/16.jpg)
What about scopes?
Rails 2x
Rails 3
![Page 17: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/17.jpg)
Reusing scopes
![Page 18: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/18.jpg)
Merging scopes
![Page 19: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/19.jpg)
Using scoped
![Page 20: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/20.jpg)
Mix and match
![Page 21: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/21.jpg)
Behaves like the model
newcreatecreate!finddestroydestroy_all
deletedelete_allupdateupdate_allexists?
![Page 22: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/22.jpg)
Building with where values
![Page 23: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/23.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/24.jpg)
Plays nice with caching
![Page 25: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/25.jpg)
Modeling with class
![Page 26: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/26.jpg)
to_sql
![Page 27: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/27.jpg)
where_values
![Page 28: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/28.jpg)
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 29: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/29.jpg)
MetaSearch & MetaWhere
http://github.com/ernie/meta_where
Ernie Miller
http://github.com/ernie/meta_search
![Page 30: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/30.jpg)
Got some hotness
• new engine
• concise and readable
• simpler to model
• flexible
• extendable
![Page 31: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/31.jpg)
Go have fun, get dirty
![Page 32: Active record makeover](https://reader033.vdocuments.mx/reader033/viewer/2022042614/554f49bab4c905b9508b47b7/html5/thumbnails/32.jpg)
Thank youMarty Haught
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