Reputation: 11
I have this migration to the table users made with Devise, all worked fine until i tried to make a back office with active_admin gem and added a role, etc:
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
And this migration that add a column to the table:
class AddRoleToUsers < ActiveRecord::Migration
def change
add_column :users, :role, :string
User.create! do |u|
u.username = 'admin'
u.email = '[email protected]'
u.password = 'admin123'
u.role = 'administrator'
end
end
end
I do rake db:migrate and got this error:
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'
........
I tried rake db:rollback and got a similar error. What could it be?
EDIT: 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
end
Upvotes: 0
Views: 437
Reputation: 2289
Use find or create method. This will allow you to not raise invalid record error if you already have user, so all later migrations will run well.
Upvotes: 0
Reputation: 3803
class AddRoleToUsers < ActiveRecord::Migration
def change
add_column :users, :role, :string
User.reset_column_information
u = User.new
u.username = 'admin'
u.email = '[email protected]'
u.password = 'admin123'
u.role = 'administrator'
u.save(:validate => false)
end
end
Upvotes: 0
Reputation: 11076
I believe you need to call User.reset_column_information
in your migration, after adding the new column, otherwise Rails doesn't know about it.
Upvotes: 0