当你有 mongodb 时,是否应该使用 rake db:migrate

Should one use rake db:migrate when you have mongodb

我正在创建这样的新迁移:

rails g migration add_confirmable_to_devise

它正在生成文件:db/migrate/YYYYMMDDxxx_add_confirmable_to_devise.rb

然后我添加:

class AddConfirmableToDevise < ActiveRecord::Migration
  # Note: You can't use change, as User.update_all will fail in the down migration
  def up
    add_column :users, :confirmation_token, :string
    add_column :users, :confirmed_at, :datetime
    add_column :users, :confirmation_sent_at, :datetime
    # add_column :users, :unconfirmed_email, :string # Only if using reconfirmable
    add_index :users, :confirmation_token, unique: true
    # User.reset_column_information # Need for some types of updates, but not for update_all.
    # To avoid a short time window between running the migration and updating all existing
    # users as confirmed, do the following
    User.update_all confirmed_at: DateTime.now
    # All existing user accounts should be able to log in after this.
  end

  def down
    remove_columns :users, :confirmation_token, :confirmed_at, :confirmation_sent_at
    # remove_columns :users, :unconfirmed_email # Only if using reconfirmable
  end
end

然后我 rake db:migrate

我明白了 Process finished with exit code 0

但是当我尝试对其进行测试以进行注册时,我得到:undefined local variable or method 'confirmed_at' for #User

我正在按照这些说明进行操作 here

是不是因为我有mongodb?在 mongodb 中有不同的做法吗?

摘自RailsApp GitHub Issue

When you use Mongoid and MongoDB, there is no need to run rake db:migrate. An advantage of using MongoDB is there is no need for migrations or schemas.

来自Mongo Doc:

Mongoid does not utilize ActiveRecord migrations, since MongoDB does not require a schema to be defined prior to storing data.

使用MongoDB时,只需在模型上配置字段即可。迁移(和架构)用于 SQL 数据库。