Home > Blockchain >  Ruby on Rails - Is it possible to add a costum column whenever I create a New Table?
Ruby on Rails - Is it possible to add a costum column whenever I create a New Table?

Time:07-01

Whenever I create a table for User, it always have a ID column. I want that whenever I create a new table, a costum_column (uuid, in my case) always be added as a column, just like the ID.

CodePudding user response:

(assuming you are talking about ruby on rails)

if you want the id to be a uuid the easiest is to have the following

class MyMigration < ActiveRecord::Migration
  create_table "my_table", id: :uuid do |t|
    # ...
  end
end

If you want all create_table to ALWAYS add another custom column, such as public_id, then you could add a monkey patch to your create_table method

# initializers/custom_table_column.rb
module ActiveRecord
  class Migration
    class Current
      module CustomColumnOnCreateTable
        def create_table(*args)
          add_custom_column = ->(t) { t.uuid("public_id", null: false) } # for example

          if block_given?
            super do |t|
              add_custom_column.call(t)
              yield compatible_table_definition(t)
            end
          else
            super { |t| add_custom_column.call(t) }
          end
        end
      end

      prepend CustomColumnOnCreateTable
    end
  end
end

The issue with this solution IMO is that you are hiding things. People (collaborators or colleagues, especially more junior ones) who use the method are not expecting it and will search hours from where this unexpected behavior come from

IMO a better alternative is to create a helper that would allow to still manually have to call it, but in one line, or with a simple method call. so nothing is hidden, but it's easy.

module ActiveRecord
  class Migration
    class Current
      module CustomColumnOnCreateTable
        def add_public_id_column(t)
          t.uuid("public_id", null: false) # for example
        end
      end

      prepend CustomColumnOnCreateTable
    end
  end
end

then you can just

# my migration
# ...
  def up
    create_table :test, id: :uuid do |t|
      add_public_id_column(t)
      t.string :something_else
      # ...
    end
  end
  • Related