使用户admin

时间:2015-04-23 16:49:57

标签: ruby-on-rails ruby admin

昨晚我试图编辑我的一些迁移以在heroku上推送应用程序时,我意外地跑了db:loadschema

管理员用户非常适合'admin@gmail.com'

现在当我rake db:migrate我的迁移再次出现错误时

== 20150404180803 UpdateUsers: migrating ======================================
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

undefined method `update_attributes' for nil:NilClass/home/ubuntu/workspace/hrApp/db/migrate/20150404180803_update_users.rb:7:in `change'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:606:in `exec_migration'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:590:in `block (2 levels) in migrate'

class AddAdminToUsers < ActiveRecord::Migration
  def change
    add_column :users, :admin, :boolean, :default => false
  end
end

class UpdateUsers < ActiveRecord::Migration
  def change
    @u = User.find_by( email: 'admin@gmail.com')
    @u.update_attribute :admin , true
  #@u.update_attributes(:admin ,true) 
  end
end

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, 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

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      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
  end
end

任何关于什么导致此错误的想法?

感谢注入。我已经尝试进入用户模型

 @u = User.new(:email => "admin@gmail.com")
  @u.save

发现错误

rake aborted!
ActiveRecord::StatementInvalid: Could not find table 'users'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:517:in `table_structure'

抱歉应该是

    @u = User.new(email: 'admin@gmail.com')
    @u.save

得到错误

rake aborted!
ActiveRecord::StatementInvalid: Could not find table 'users'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:517:in `table_structure'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:389:in `columns'

1 个答案:

答案 0 :(得分:1)

迁移期望数据库中的用户的电子邮件地址为“admin@gmail.com”:

@u = User.find_by( email: 'admin@gmail.com')
@u.update_attribute :admin , true

如果该用户不存在,那么@u将为零,您将收到您所看到的错误。将该用户的记录添加到数据库,然后重新运行迁移。