Date: Apr 25, 2021

It's been a while since I used Ruby on Rails (RoR) framework. In fact, it's been a while since I used Ruby. The last time I can remember using Ruby was to create Logstash plugin.

Recently, a new platform similar to Heroku has been released: railway.app. It allows you deploy containerized apps as well as its stateful components such as database (Postgres or MySQL) and caching (Redis).

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/f41ce34c-365a-4bfd-b143-85cb678ceb67/Screen_Shot_2021-04-25_at_14.14.41.png

The platform name (railway.app) is tempting me to deploy a rails app just so I can say "Rails on Rail". So I created a github repo and deployed it using railway.app at https://rails-on-rail-production.up.railway.app. The deployment process (rails v6.1.3 + postgres) is quite straightforward and took me around 10 minutes.

My "Rails on Rail" deployment. Choo Choo!

My "Rails on Rail" deployment. Choo Choo!

While fiddling with Rails on Rail, I took some time to revisit Rails (as a framework). Here's what I noted:

Mental Model

  1. Just like any other frameworks (Laravel, Django), there are conventions (aka "magics") that you need to be aware. Knowing these will help you navigate the framework and reason about how things work.

  2. Watch out for N+1 queries. FWIW, this applies to all situations where you use ORM regardless of framework choice.

  3. Use ActiveSupport::CurrentAttributes to add attributes that persist in request's lifetime (per-request attributes). See: https://fullstackheroes.com/rails/current-attributes/.

  4. Unlike PHP or Node, you don't need to use use or require or import keywords to use packages (gems).

  5. Starting from Rails 6.1, you can use signed_id to create a verifiable id of related model. Example:

    # Create signed token
    user = User.find(1)
    signed_user_id = user.signed_id(purpose: :password_reset, expires_in: 15.minutes)
    
    # Find user by signed token
    User.find_signed(signed_user_id)
    
  6. You can use binding.irb as debugger. See: https://jemma.dev/blog/binding-irb

  7. rescue is the equivalent of catch in languages that support try and catch exception. See: https://crodrigues.com/try-catch-finally-equivalent-in-ruby/

  8. Form error is stored in model instance

  9. Usebundle add <package_name> to add gem to your project (equivalent to yarn add <package_name> in nodejs)

  10. It's quite common for rails packages to have the accompanying rails :install command. Example:

    bundle add hotwire-rails # add hotwire gem
    rails hotwire:install # install hotwire
    
  11. Use rails credentials:edit to manage your encrypted configs