update_attribute 将用户设置为管理员时出错

update_attribute error when making user admin

我 运行 昨天晚上我试图编辑我的一些迁移以将应用程序推送到 heroku 时不小心 db:loadschema

admin 用户在 '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'

迁移预计数据库中有一个电子邮件地址为 'admin@gmail.com':

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

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