【问题标题】:Rails weird behaviour with rails destroy migrationRails 的 Rails 奇怪行为破坏了迁移
【发布时间】:2015-10-12 16:36:20
【问题描述】:

在 Rails 上工作时,我注意到 Rails 迁移的以下行为。我不知道它是否是预期的行为。有人知道吗?

第 1 步 - 我创建了一个迁移

rails g migration CreateCustomer

此命令的结果 -

  invoke  active_record
  create  db/migrate/20151012160803_create_customer.rb

第 2 步 - 现在我想撤消使用上述命令生成的代码。

rails d migration CreateCustomer

此命令的结果 -

  invoke  active_record
  remove  db/migrate/20151012160803_create_customer.rb

第 3 步 - 如果我再次运行第 2 步命令rails d migration CreateCustomer,我会在不同的 Rails 版本上得到不同的结果

Rails 3.2.17 上的结果是 -

  invoke  active_record
  remove  migration.rb

Rails 4.0.0 上的结果是 -

  invoke  active_record
  remove  create_table_migration.rb

我的问题是为什么要删除 migration.rbcreate_table_migration.rb 文件? rails 真的会删除这些文件吗?

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 rubygems


    【解决方案1】:

    不,如果您将运行它并不会真正删除此文件:

    rails d migration SomeUnRealMigration

    你会得到同样的错误,你看到的那两个文件名是 Rails create_table_migration.rb 的核心文件:

        class <%= migration_class_name %> < ActiveRecord::Migration
          def change
            create_table :<%= table_name %> do |t|
            t.string :email,                null: false
            t.string :password_digest,      null: false
            t.string :password_reset_token, null: false, limit: 60
            <% attributes.each do |attribute| -%>
              <% if attribute.password_digest? -%>
                t.string :password_digest<%= attribute.inject_options %>
              <% else -%>
                t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
              <% end -%>
           <% end -%>
           <% if options[:timestamps] %>
             t.timestamps
           <% end -%>
         end
        <% attributes_with_index.each do |attribute| -%>
          add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
        <% end -%>
        end
      end
    

    这不会影响您应用程序中的任何文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      • 2017-09-19
      • 2018-04-30
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多