Michael Wong
Michael Wong

Reputation: 11

RUBY - Couldn't find User with 'id'=1

I am currently receiving this error:

ActiveRecord::RecordNotFound in MainController#index

After I destroyed a preference, which was held by myself.

Error:

ActiveRecord::RecordNotFound in MainController#index

Couldn't find User with 'id'=1

def get_owner
    return User.find( self.owner );  // LINE WITH ERROR
end

Here is post.rb:

class Post < ActiveRecord::Base
  enum status: [ :ps_normal, :ps_locked, :ps_blocked, :ps_protected ]
  enum sortable: [ :school, :company, :date ]

  validates :owner, presence: true

  def owner_name
    return self.get_owner.display_name;
  end

  def get_owner
    return User.find( self.owner );
  end

  def readable?
    return (self.status != :blocked ) ? true : false;
  end
end

UPDATE#1

2.3.0 :001 > User.find(1)
User Load (1.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=1
    from /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/finder_methods.rb:324:in `raise_record_not_found_exception!'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/finder_methods.rb:444:in `find_one'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/finder_methods.rb:423:in `find_with_ids'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/finder_methods.rb:71:in `find'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.6/lib/active_record/querying.rb:3:in `find'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.6/lib/active_record/core.rb:131:in `find'
    from (irb):1
    from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands/console.rb:110:in `start'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands/console.rb:9:in `start'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

How should I fix this? Add userid=1?

Update #2

I deleted my own preference under my own username and I received this error. People are saying I am missing id=1, but I am unsure on how to add it back into the database

Upvotes: 0

Views: 966

Answers (1)

Lahiru Jayaratne
Lahiru Jayaratne

Reputation: 1762

As per your console output, there is no user with the id of 1. So if you need a user with id=1 you can do it like this.

First, create a user with the same details which you had for the deleted user. After the user successfully created then go to Rails console and:

u = User.last
# => #<User id: x, ...... >
u.update_column(:id, 1)

Please refer to Rails API doc here for more info. Choose according to the Rails version that you are using.

Upvotes: 1

Related Questions