Answers · Deploying

How do I open a Rails console on a production server?

Run kamal app exec --interactive --reuse 'bin/rails console'. That attaches to the running container rather than booting a new one, so you see live data. One Shot ships no admin UI on purpose, so the console is the supported way to inspect and fix records, and everything you touch must go through an Account.

The command

bin/kamal app exec --interactive --reuse 'bin/rails console'

--reuse is the part that matters. Without it Kamal starts a fresh container, which works but boots slower and, more importantly, is not the process serving traffic. With it you are inside the live container.

For a one-off that does not need a prompt, skip the console entirely:

bin/kamal app exec 'bin/rails runner "puts Account.count"'

Everything is account scoped

This is the rule the console does not enforce for you. Customer data belongs to an Account, and queries go through it:

account = Account.find_by(name: "Acme")
account.projects.count

Reaching for a bare model class works and is exactly how you leak one customer's rows into another customer's view when you paste the result somewhere. Operate through the account.

To act as a signed-in user, create a session rather than stubbing Current:

Current.session = User.find_by(email_address: "[email protected]").sessions.create!

Before you run a write

Two habits worth keeping. Read the query back before you run the update, and prefer a scoped update! over update_all, which skips validations and callbacks silently.

account.projects.where(archived: true).count   # look first

When the console is the wrong tool

Anything you will do more than twice belongs in a rake task in lib/tasks, where it can be read, reviewed and tested. The console is for the one-off and the investigation. A recurring data fix run by hand at a prompt is a bug report waiting to happen.

Anything that should happen on a schedule belongs in a job instead, enqueued with perform_later and listed in config/recurring.yml. Solid Queue runs it inside the same container, so there is no separate worker host to keep alive and nothing extra to deploy.

Related questions

How do I deploy a Rails 8 app without Heroku?

Use Kamal, which ships with Rails 8. You give it a server over SSH and a container registry in config/deploy.yml, run kamal setup once, and kamal deploy after that. It builds an image, pushes it, and swaps containers with no downtime. A $5 VPS replaces the PaaS.

How do I deploy a Rails app from GitHub Actions?

Run kamal deploy as a workflow step. Kamal needs three things in the runner: an SSH key that can reach the server, registry credentials, and RAILS_MASTER_KEY. All three go in as repository secrets and are read by .kamal/secrets. Gate the deploy job behind the test job so a red build never ships.

How do I fix no space left on device on a deploy server?

Old container images are the cause, not your app. Every deploy pushes a new image and the server keeps the old ones. Run docker image prune -a -f to reclaim the space, then check with df -h. On a 25GB VPS this usually returns several gigabytes, and it is worth doing before you are locked out rather than after.