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).
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!
While fiddling with Rails on Rail, I took some time to revisit Rails (as a framework). Here's what I noted:
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.
Watch out for N+1 queries. FWIW, this applies to all situations where you use ORM regardless of framework choice.
ActiveSupport::CurrentAttributes to add attributes that persist in request's lifetime (per-request attributes). See: https://fullstackheroes.com/rails/current-attributes/.
Unlike PHP or Node, you don't need to use
import keywords to use packages (gems).
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)
You can use
binding.irb as debugger. See: https://jemma.dev/blog/binding-irb
rescue is the equivalent of
catch in languages that support
catch exception. See: https://crodrigues.com/try-catch-finally-equivalent-in-ruby/
Form error is stored in model instance
bundle add <package_name> to add gem to your project (equivalent to
yarn add <package_name> in nodejs)
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
rails credentials:edit to manage your encrypted configs