Active Record causes numerous problems within a Rails application. I cover a few of these in the talk, and show an alternate way of approaching the same problems.
ExplodingRails
View Slide
Rails:“Ge2ng started is easy”True / False?
Rails:“Ge2ng started is easy”True
Rails:“Applica=on maintenanceis a cinch.”True / False?
Rails:“Applica=on maintenanceis a cinch.”False
Ge2ng started is aone +me cost.Maintenance is forever.
Ac=ve Recordis (mostly) to blame.
“Look at all the thingsI’m not doing.”“Provide sharp knives.”
Causes an N+1 query
Mixes database queryingand data manipula=on
DB Calls Count: 3
“I’ll just stub thosethings out!”
Fixes the N+1 issue
Causes an N+1 query, also
SQL QueryA viewNew in Rails 5.2Query logging
Ac=ve Recordis an an+-paBern.
Ac=ve Recordviolates SRPYou, looking on in despair
Ac=ve Record =esyour applica=onto your database.
Rails applica=ons knowtoo much about theirunderlying database.(because Ac=ve Record makes it easy)
Rails hasbarely evolved.
Rails 1.2 Rails 5.2app/controllers/helpers/models/viewsapp/controllers/helpers/mailers/models/views
Does it go in theview?Does it go in thecontroller?Put it in the modelNoNo
…
Have we learnednothing?
Rails as adelivery mechanism.
A browserRails AppReposDatabaseRailsRubyPostgreSQL(duh)POROsKnows nothing about
Makes arequest to…A browserRails AppReposDatabasePOROs
A browserRails AppReposDatabasePOROsMakes arequest to…
A browserRails AppReposDatabasePOROsReturnsrecords
Returns en++esA browserRails AppReposDatabasePOROs
ReturnsdataA browserRails AppReposDatabasePOROs
An En=ty
Projects::Project.new# => 12 methodsMy Rails App
Project.new# => 296 methodsA Vanilla Rails Appcallbacks, dirty tracking, validations, etc.
DB Calls Count: 0
My Rails App
Regular Rails App
How did I get to thismagical utopia?
ROM!http://rom-rb.org/
Exploded Railsapp/controllers/helpers/models/relations/repositories/schemas/transactions/viewsRegular Rails 5.2app/controllers/helpers/mailers/models/views
Repositoriesare the APIbetween rela=ons andyour applica=on.
Rela=onsare the APIbetween repositoriesand your database.
Schemasvalidate datafor your applica=on.
Transac=onswrap complicatedac=ons into sensible steps.
A cleanerarchitecture.
Rails controllersknow nothingabout the database.(They only know about the repositories)
Rails viewsare unable toperform DB queries.(They accept what is given to them)
Rails modelsrepresent data anddo nothing else.(No callbacks. No validation.)
Rails as a simpledelivery mechanism.
I could go on…
Or you could readmy app’s code:bit.ly/twist-v2Uses Hanami + React
Or you could readmy new book:bit.ly/exploding-rails$10 offCovers using rom-rband dry-rb with Rails