我试图跟随Learn Ruby on Rails tutorial我的Michael Hartl。我在第7章,它从数据库中获取用户数据以显示页面。但是,我一直收到一个错误,即没有ID为1的用户。我无法验证数据库中的数据(因为sql数据库查看器说它无法打开文件,我不知道如何安装据称修复该问题的1.1版本)。
我尝试在控制台中添加一个用户并且它似乎成功了,但是第二个我去找我刚刚创建的用户,它不存在(见下文)。有帮助吗?我此刻非常沮丧。
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>'
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...">
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>'
答案 0 :(得分:1)
我想,您已经在命令行中运行了'rake db:migrate',并且已经在db文件夹中生成了sqlite3数据库文件。
答案 1 :(得分:1)
我假设你使用的是rails控制台,
如果是这样,一旦将用户添加到数据库,您需要重新加载控制台
在你的rails控制台中
User.create(name: "MD", email: "md@example.com", password: "foobar", password_confirmation: "foobar")
reload!
User.find(1)
答案 2 :(得分:1)
正如@aceofbassgreg所提到的,你运行的User.create方法创建了一个ID为4的用户。
在您的教程中,您可能已创建并删除了一些用户实例。每次创建新用户时,ID字段的值都会自动生成,并且会不断增加。它也是主要关键。
ID列具有auto_increment属性。它由RDBMS自动确定,每次创建对象的新实例时它都会递增1。因此,如果您创建3个User对象然后删除它们,即使您的表为空,下一个User对象的ID也是4.当然,您可以使用正确的SQL命令将auto_increment计数器重置为任何值。 / p>
除此之外,出于安全原因,create方法在将它们作为参数传递时(“质量分配”)忽略主键(此键中的ID)和继承字段。因为User.create(:id =&gt; 1)不起作用。