Ruby on Rails-是否可以在我创建新桌子时添加服装列?

发布于 2025-02-11 06:57:23 字数 73 浏览 0 评论 0原文

每当我为用户创建表格时,它总是具有ID列。我希望每当我创建一个新表时,就始终像ID一样添加Codum_column(在我的情况下)。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑾夏年华 2025-02-18 06:57:24

(假设您在轨道上谈论Ruby)

如果您希望ID成为UUID,则最简单的是,

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

如果您希望所有create_table都可以添加另一个自定义列,例如public_id,那么您可以将猴子补丁添加到您的create_table方法

# 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

此解决方案IMO的问题是您正在隐藏事物。使用该方法的人(合作者或同事,尤其是更多的初级同事)并不期待它,并且会从

IMO出现的意外行为来搜索一个更好的选择是创建一个助理,该助手仍然可以手动地称呼它,但是一行或使用简单的方法调用。所以没有什么隐藏的,但这很容易。

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

那你就可以

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

(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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文