我将此迁移到使用Devise制作的用户表,所有工作正常,直到我尝试使用active_admin gem创建后台并添加角色等:
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
## Database authenticatable
t.string :username, null: false, default: ""
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, null: false
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
t.timestamps
end
add_index :users, :email, unique: true
add_index :users, :username, unique: true
add_index :users, :reset_password_token, unique: true
add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
这个向表中添加列的迁移:
class AddRoleToUsers < ActiveRecord::Migration
def change
add_column :users, :role, :string
User.create! do |u|
u.username = 'admin'
u.email = 'admin@example.com'
u.password = 'admin123'
u.role = 'administrator'
end
end
end
我做rake db:migrate并得到了这个错误:
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
Record invalido/home/dev8/.rvm/gems/ruby-2.2.1/gems/activerecord- 4.2.1/lib/active_record/validations.rb:79:in `raise_record_invalid'
/home/dev8/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/validations.rb:43:in `save!'
/home/dev8/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
/home/dev8/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/transactions.rb:291:in `block in save!'
/home/dev8/.rvm/gems/ruby-2.2.1/gems/activerecord- 4.2.1/lib/active_record/transactions.rb:347:in `block in with_transaction_returning_status'
/home/dev8/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
/home/dev8/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/transactions.rb:220:in `transaction'
/home/dev8/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/transactions.rb:344:in `with_transaction_returning_status'
/home/dev8/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/transactions.rb:291:in `save!'
/home/dev8/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/persistence.rb:51:in `create!' /home/dev8/RedTwitter/db/migrate/20150605140226_add_role_to_users.rb:6:in `change'
/home/dev8/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/migration.rb:606:in `exec_migration'
/home/dev8/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/migration.rb:590:in `block (2 levels) in migrate'
/home/dev8/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/migration.rb:589:in `block in migrate'
/home/dev8/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
........
我试过rake db:rollback并得到了类似的错误。 它可能是什么?
编辑: User.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :registerable, :confirmable, :authentication_keys => [:login]
attr_accessor :login
has_many :tweets , dependent: :destroy
validates :username,
:presence => true,
:uniqueness => {
:case_sensitive => false
}, length: { in: 6..20 }
def role?(role)
roles.include? role.to_s
end
def login=(login)
@login = login
end
def login
@login || self.username || self.email
end
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions.to_hash).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions.to_hash).first
end
end
端
答案 0 :(得分:0)
我认为在添加新列后,您需要在迁移中调用User.reset_column_information
,否则Rails不了解它。
答案 1 :(得分:0)
class AddRoleToUsers < ActiveRecord::Migration
def change
add_column :users, :role, :string
User.reset_column_information
u = User.new
u.username = 'admin'
u.email = 'admin@example.com'
u.password = 'admin123'
u.role = 'administrator'
u.save(:validate => false)
end
end
答案 2 :(得分:0)
使用find or create方法。如果您已经拥有用户,这将允许您不会引发无效记录错误,因此所有以后的迁移都将运行良好。