1

Im trying to follow the Learn Ruby on Rails tutorial my Michael Hartl. I'm on chapter 7 which grabs user data from a database to display pages. However, I keep getting an error that there is no user with an ID of one. I can't verify the data in the database (since the sql database viewer says it cant open the file and I have no idea how to install the 1.1 version which allegedly fixes that problem).

I tried adding a user in the console and it seems to succeed, but the second I go to find the user I just created, it doesn't exist (see below). Any help? I'm very frustrated at the moment.

User.find

1.9.3-p327 :001 > User.find(1)
  User Load (31.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
ActiveRecord::RecordNotFound: Couldn't find User with id=1
    from /home/gerard/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/relation/finder_methods.rb:341:in `find_one'
    from /home/gerard/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/relation/finder_methods.rb:312:in `find_with_ids'
    from /home/gerard/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/relation/finder_methods.rb:107:in `find'
    from /home/gerard/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/querying.rb:5:in `find'
    from (irb):1
    from /home/gerard/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
    from /home/gerard/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
    from /home/gerard/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

User.create

1.9.3-p327 :002 > User.create(name: "MD", email: "md@example.com", password: "foobar", password_confirmation: "foobar")
   (22.6ms)  SAVEPOINT active_record_1
  User Exists (56.7ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('md@example.com') LIMIT 1
  SQL (153.7ms)  INSERT INTO "users" ("created_at", "email", "name", "password_digest", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["created_at", Mon, 11 Feb 2013 00:59:52 UTC +00:00], ["email", "md@example.com"], ["name", "MD"], ["password_digest", "$2a$10$NFEb9PsPQuSEGbJ8.TOILefjZMgTAJXCFHExZpGZmlov72UffjoUe"], ["updated_at", Mon, 11 Feb 2013 00:59:52 UTC +00:00]]
   (0.4ms)  RELEASE SAVEPOINT active_record_1
 => #<User id: 4, name: "MD", email: "md@example.com", created_at: "2013-02-11 00:59:52", updated_at: "2013-02-11 00:59:52", password_digest: "$2a$10$NFEb9PsPQuSEGbJ8.TOILefjZMgTAJXCFHExZpGZmlov..."> 

User.find

1.9.3-p327 :003 > User.find(1)
  User Load (2.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
ActiveRecord::RecordNotFound: Couldn't find User with id=1
    from /home/gerard/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/relation/finder_methods.rb:341:in `find_one'
    from /home/gerard/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/relation/finder_methods.rb:312:in `find_with_ids'
    from /home/gerard/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/relation/finder_methods.rb:107:in `find'
    from /home/gerard/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/querying.rb:5:in `find'
    from (irb):3
    from /home/gerard/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
    from /home/gerard/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
    from /home/gerard/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
4

3 回答 3

1

我假设您正在使用 rails 控制台,

如果是这样,一旦将用户添加到数据库,则需要重新加载控制台

在您的 Rails 控制台中

User.create(name: "MD", email: "md@example.com", password: "foobar", password_confirmation: "foobar")

reload!

User.find(1)
于 2013-02-11T03:50:34.283 回答
1

I guess, you've already run a 'rake db:migrate' in your command line and your sqlite3 database files already generated in your db folder.

于 2013-02-11T01:22:18.200 回答
1

正如@aceofbassgreg 提到的,您运行的 User.create 方法创建了一个 ID 为 4 的用户。

在您的教程中,您可能已经创建和删除了一些用户实例。每次创建新用户时,都会自动生成 ID 字段的值并不断增加。它也是主键。

ID 列具有 auto_increment 属性。它由 RDBMS 自动确定,并且每次创建对象的新实例时都会增加 1。因此,如果您创建 3 个 User 对象然后删除它们,即使您的表是空的,下一个 User 对象的 ID 也会为 4。当然,您可以使用正确的 SQL 命令将 auto_increment 计数器重置为任何值。

最重要的是,出于安全原因,当您将主键(此键中的 ID)和继承字段作为参数(“批量分配”)传递时,create 方法会忽略它们。因为那个 User.create(:id => 1) 将不起作用。

于 2013-02-12T04:23:07.217 回答