Unable to Get the Data from SQLite.

Hi Everyone,

   I am going through a tutorial of Rails, I have created a database, models and controllers. In the example it says when entering the

http://localhost:3000/users/1

URL in the browser it should show the first record in the database in the browser.

http://ruby.railstutorial.org/chapters/modeling-and-viewing-users-one#sec:model_annotation

I get the exception: "Couldn't find User with id=1"

But when i check through the ruby console, User.find(1) returns a proper column.

I am on 3.1.3 and the tutorial is on 3.0.11. I was thinking it could be something to do with that. I had asked one question yesterday and it turned out to be a version change problem.

Can you copy and paste the relevant bit from log/development.log when you click the link and also the console output from User.find(1). Use Copy/paste the console output also, including the command you entered, do not retype it here.

Colin

Started GET "/users/1" for 127.0.0.1 at 2011-11-29 17:19:31 +0530   Processing by UsersController#show as HTML   Parameters: {"id"=>"1"}   User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "1"]] Completed 404 Not Found in 15ms

ActiveRecord::RecordNotFound (Couldn't find User with id=1):   app/controllers/users_controller.rb:4:in `show'

Rendered C:/Ruby193/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.0ms) Rendered C:/Ruby193/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (30.0ms) Rendered C:/Ruby193/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (45.1m)

Now the strange thing is when i enter the find command i get an exception! Previously it was working fine.

irb(main):002:0> User.new => #<User id: nil, name: nil, email: nil, created_at: nil, updated_at:

irb(main):003:0> user = User.new(:name => "Michael Hartl", :email => "mhartl@example.com") => #<User id: nil, name: "Michael Hartl", email: "mhartl@example.com", created_at: nil, updated_at: nil>

irb(main):006:0> user.save   ←[1m←[35m (0.0ms)←[0m SAVEPOINT active_record_1   ←[1m←[36m (0.0ms)←[0m ←[1mSELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('mhartl@example.com') LIMIT 1←[0m   ←[1m←[35mSQL (0.0ms)←[0m INSERT INTO "users" ("created_at", "email", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Tue, 29 Nov 2011 11:53:29 UTC +00:00], ["email", "mhartl@example.com" ], ["name", "Michael Hartl"], ["updated_at", Tue, 29 Nov 2011 11:53:29 UTC +00:00]]   ←[1m←[36m (0.0ms)←[0m ←[1mRELEASE SAVEPOINT active_record_1←[0m => true

irb(main):012:0> user.find(1) NoMethodError: undefined method `find' for #<User:0x1d401b0>         from C:/Ruby193/gems/activemodel-3.1.3/lib/active_model/attribute_methods.rb:385:in `method_missing'         from C:/Ruby193/gems/activerecord-3.1.3/lib/active_record/attribute_methods.rb:60:in `method_missing'         from (irb):12         from C:/Ruby193/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'         from C:/Ruby193/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'         from C:/Ruby193/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'         from script/rails:6:in `require'         from script/rails:6:in `<main>'

Why doesn't this site have something like code tags or something?!!! :slight_smile:

Started GET "/users/1" for 127.0.0.1 at 2011-11-29 17:19:31 +0530 Processing by UsersController#show as HTML Parameters: {"id"=>"1"} User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "1"]] Completed 404 Not Found in 15ms

ActiveRecord::RecordNotFound (Couldn't find User with id=1): app/controllers/users_controller.rb:4:in `show'

Can you show us the code for users_controller show method.

Rendered C:/Ruby193/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.0ms) Rendered C:/Ruby193/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (30.0ms) Rendered C:/Ruby193/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (45.1m)

Now the strange thing is when i enter the find command i get an exception! Previously it was working fine.

...

irb(main):012:0> user.find(1)

That should be User.find, you are calling find on the user object you just created, instead of on the User class.

Colin

irb(main):012:0> user.find(1) NoMethodError: undefined method `find' for #<User:0x1d401b0> from

You should "find" the class, not the instance:

User.find(1)

Why doesn't this site have something like code tags or something?!!! :slight_smile:

You're posting from ruby-forum, a forum-like bridge to Ruby on Rails mailing list -- is not a forum.

irb(main):003:0> user = User.new(:name => "Michael Hartl", :email => "mhartl@example.com") => #<User id: nil, name: "Michael Hartl", email: "mhartl@example.com", created_at: nil, updated_at: nil>

irb(main):006:0> user.save

irb(main):012:0> user.find(1) NoMethodError: undefined method `find' for #<User:0x1d401b0>

  User.find(1)

Note the case sensitivity - you were calling a method "find" on an instance of the User model, not the User class method "find".

Why doesn't this site have something like code tags or something?!!! :slight_smile:

This "site" is a mailing list...

Also, can you provide the output to calling the following in a console:   User.first

(I have an inkling that your first record doesn't have an id of "1")

$ rails console --sandbox Loading development environment in sandbox (Rails 3.1.3) Any modifications you make will be rolled back on exit irb(main):001:0> User.new => #<User id: nil, name: nil, email: nil, created_at: nil, updated_at:

irb(main):002:0> User.first   ←[1m←[36mUser Load (0.0ms)←[0m ←[1mSELECT "users".* FROM "users" LIMIT 1←[0m => nil irb(main):003:0> user = User.new(:name => "Michael Hartl", :email => "mhartl@example.com") => #<User id: nil, name: "Michael Hartl", email: "mhartl@example.com", created_at: nil, updated_at: nil> irb(main):004:0> user.save   ←[1m←[35m (0.0ms)←[0m SAVEPOINT active_record_1   ←[1m←[36m (0.0ms)←[0m ←[1mSELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('mhartl@example.com') LIMIT 1←[0m   ←[1m←[35mSQL (15.5ms)←[0m INSERT INTO "users" ("created_at", "email", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Tue, 29 Nov 2011 12:28:21 UTC +00:00], ["email", "mhartl@example.com "], ["name", "Michael Hartl"], ["updated_at", Tue, 29 Nov 2011 12:28:21 UTC +00:00]]   ←[1m←[36m (0.0ms)←[0m ←[1mRELEASE SAVEPOINT active_record_1←[0m => true irb(main):005:0> User.first   ←[1m←[35mUser Load (0.0ms)←[0m SELECT "users".* FROM "users" LIMIT 1 => #<User id: 1, name: "Michael Hartl", email: "mhartl@example.com", created_at: "2011-11-29 12:28:21", updated_at: "2011-11-29 12:28:21">

users_controller.rb:

class UsersController < ApplicationController

  def show     @user = User.find(params[:id])   end

  def new     @title = "Sign Up"   end

end

ermm.... "sandbox"? :wink:

By running it in a sandbox the code above is creating the user with id 1, but it gets removed when you close the console, so when the app runs there are no users.

To check this just run rails console User.find(1)

Colin

OK.. I will give it a try!

I think i have to redo all the commands and add data all over again! Currently i get:

$ rails console Loading development environment (Rails 3.1.3) irb(main):001:0> User.find(1)   ←[1m←[36mUser Load (31.2ms)←[0m ←[1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1←[0m [["id", 1]] ActiveRecord::RecordNotFound: Couldn't find User with id=1

But how come coming out of the sandbox console deletes data from the database? Database values should be persistent right? That is the whole idea of a DB, or am i missing something here?

Yes - you're missing the point that a "sandbox" is there precisely to revert the data you've been fiddling with back to what it was at the start. If you want to persist your changes, don't use the sandbox. If you want to fiddle and do no damage, use the sandbox :slight_smile:

OK.. I will give it a try!

I think i have to redo all the commands and add data all over again!

If you have a lot of data to load manually you can do it by putting code in db/seeds.rb then running rake db:seed

... But how come coming out of the sandbox console deletes data from the database? Database values should be persistent right? That is the whole idea of a DB, or am i missing something here?

Did you not see the comment displayed when you opened the console in a sandbox? $ rails console --sandbox Loading development environment in sandbox (Rails 3.1.3) Any modifications you make will be rolled back on exit

That is the whole point of the sandbox, to play without affecting the database.

Colin

I got it working now! Thanks guys! Will be back tomorrow with some other problem! :slight_smile: