“...I've been working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix and learning Rust. I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working for InPay...”

Rob Lacey (contact@robl.me)
Senior Software Engineer, Brighton, UK

Three things that were invaluable today

Quick and dirty database migration on the console

When a database migration failed to roll back properly, stripping one column but not all, we had to put back the original column without changing the migration and re-running. It always feels very dangerous when a multipart migration fails halfway through. So….you can actually call migration commands on ActiveRecord::Migration directly, just be careful to test in development first as accidents can and will happen.

Rails pro >> ActiveRecord::Migration.add_column :somethings, :thing_file_name, :string, before: :thing_file_name
-- add_column(:somethings, :thing_file_name, :string, {:before=>:splash_content_type})
   (4838.6ms)  ALTER TABLE `somethings` ADD `thing_file_name` varchar(255)

Cherry picking changes in a different branch with Git.

I had to give up on a recent branch because refactoring had caused more problems than it intended to solve. Without wanting to start over I was able to pick the files that I cared about individually rather than sifting through and reverting parts of a previous commit.

git checkout feature/somefeature -- config/blah.yml

While I’m there, ever wanted to ditch your local changes in a conflict over the merged in changes from another branch?

git checkout --theirs config/routes.rb

Rails partials as layouts

Ever had a Rails partial and wanted to re-use it in a specific situation but with a small change? A partial can also be used as a layout, it appears a partial can be a layout that takes a block.

app/views/somethings/_dave.html.haml

= form_for(@dave) do |f|
  = f.text_field :full_name

app/views/somethings/thing.html.haml

= render partial: 'dave'

I really want to add an way to to re-use the form and add to it. How about this?

app/views/somethings/_dave.html.haml

= form_for(@dave) do |f|
  - if block_given?
    = yield
  = f.text_field :full_name

app/views/somethings/thing.html.haml

= render layout: 'dave' do
  %p A MESSAGE TO PLONK AT THE TOP OF THE FORM