我无法理解迁移如何根据模型自行更新。
例如,我已经设计了设置,我想删除电子邮件字段并且只有一个用户名和密码字段。
所以我做了
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :username, :password
# attr_accessible :title, :body
end
但是,迁移文件即使在之后也不会反映这些更改
rake db:migrate
或者
rake db:reset
迁移文件仍然有电子邮件字段
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
## Database authenticatable
t.string :email, :null => false, :default => ""
t.string :encrypted_password, :null => false, :default => ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
t.integer :sign_in_count, :default => 0
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
## Token authenticatable
# t.string :authentication_token
t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :reset_password_token, :unique => true
# add_index :users, :confirmation_token, :unique => true
# add_index :users, :unlock_token, :unique => true
# add_index :users, :authentication_token, :unique => true
end
end
我还尝试删除迁移文件,以便 Rails 希望知道创建一个新文件。但是,这不起作用,并且迁移仍然是从某个地方运行的?这些迁移是从哪里运行的?请注意,我的 db/migrate 文件当时是空的。
-- create_table("users", {:force=>true})
-> 0.2476s
-- add_index("users", ["email"], {:unique=>true, :name=>"index_users_on_email"})
-> 0.1832s
-- add_index("users", ["reset_password_token"], {:unique=>true, :name=>"index_users_on_reset_password_token"})
-> 0.1832s
-- initialize_schema_migrations_table()
-> 0.3776s
-- assume_migrated_upto_version(20130320033132, ["/home/Portfolio Rails/portfolio/db/migrate"])
-> 0.1532s