Exploding Rails

Be66f1ccc28a6a3dfb248454b8513b17?s=47 Ryan Bigg
April 24, 2018

Exploding Rails

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.

Be66f1ccc28a6a3dfb248454b8513b17?s=128

Ryan Bigg

April 24, 2018
Tweet

Transcript

  1. Exploding Rails

  2. Rails: “Ge2ng started is easy” True / False?

  3. Rails: “Ge2ng started is easy” True

  4. None
  5. Rails: “Applica=on maintenance is a cinch.” True / False?

  6. None
  7. Rails: “Applica=on maintenance is a cinch.” False

  8. Ge2ng started is a one +me cost. Maintenance is forever.

  9. Ac=ve Record is (mostly) to blame.

  10. None
  11. None
  12. “Look at all the things I’m not doing.” “Provide sharp

    knives.”
  13. Causes an N+1 query

  14. Mixes database querying and data manipula=on

  15. DB Calls Count: 3

  16. “I’ll just stub those things out!”

  17. None
  18. Fixes the N+1 issue

  19. None
  20. Causes an N+1 query, also

  21. SQL Query A view New in Rails 5.2 Query logging

  22. Ac=ve Record is an an+-paBern.

  23. Causes an N+1 query, also

  24. None
  25. Ac=ve Record violates SRP You, looking on in despair

  26. Ac=ve Record =es your applica=on to your database.

  27. Rails applica=ons know too much about their underlying database. (because

    Ac=ve Record makes it easy)
  28. Rails has barely evolved.

  29. Rails 1.2 Rails 5.2 app /controllers /helpers /models /views app

    /controllers /helpers /mailers /models /views
  30. Does it go in the view? Does it go in

    the controller? Put it in the model No No
  31. Have we learned nothing?

  32. Rails as a delivery mechanism.

  33. A browser Rails App Repos Database Rails Ruby PostgreSQL (duh)

    POROs Knows nothing about
  34. Makes a request to… A browser Rails App Repos Database

    POROs
  35. A browser Rails App Repos Database POROs Makes a request

    to…
  36. A browser Rails App Repos Database POROs Makes a request

    to…
  37. A browser Rails App Repos Database POROs Returns records

  38. Returns en++es A browser Rails App Repos Database POROs

  39. Returns data A browser Rails App Repos Database POROs

  40. None
  41. An En=ty

  42. Projects::Project.new # => 12 methods My Rails App

  43. Project.new # => 296 methods A Vanilla Rails App callbacks,

    dirty tracking, validations, etc.
  44. DB Calls Count: 0

  45. My Rails App

  46. Regular Rails App

  47. My Rails App

  48. Regular Rails App

  49. How did I get to this magical utopia?

  50. ROM! http://rom-rb.org/

  51. Exploded Rails app /controllers /helpers /models /relations /repositories /schemas /transactions

    /views Regular Rails 5.2 app /controllers /helpers /mailers /models /views
  52. Repositories are the API between rela=ons and your applica=on.

  53. Rela=ons are the API between repositories and your database.

  54. Schemas validate data for your applica=on.

  55. Transac=ons wrap complicated ac=ons into sensible steps.

  56. A cleaner architecture.

  57. Rails controllers know nothing about the database. (They only know

    about the repositories)
  58. Rails views are unable to perform DB queries. (They accept

    what is given to them)
  59. Rails models represent data and do nothing else. (No callbacks.

    No validation.)
  60. Rails as a simple delivery mechanism.

  61. I could go on…

  62. Or you could read my app’s code: bit.ly/twist-v2 Uses Hanami

    + React
  63. Or you could read my new book: bit.ly/exploding-rails $10 off

    Covers using rom-rb and dry-rb with Rails