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.

On 29 November 2011 10:30, Somnath M. [email protected] wrote:

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.

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)

IRB Console:

irb(main):002:0> User.new
=> #<User id: nil, name: nil, email: nil, created_at: nil, updated_at:
nil>
irb(main):003:0> user = User.new(:name => “Michael H.”, :email =>
[email protected]”)
=> #<User id: nil, name: “Michael H.”, email: “[email protected]”,
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(‘[email protected]’) 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”, “[email protected]
], [“name”, “Michael H.”], [“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):013:0> User.find(1)
←[1m←[35mUser Load (0.0ms)←[0m SELECT “users”.* FROM “users” WHERE
“users”.“id” = ? LIMIT 1 [[“id”, 1]]
=> #<User id: 1, name: “Michael H.”, email: “[email protected]”,
created_at: “2011-11-29 11:53:29”, updated_at: “2011-11-29 11:53:29”>
irb(main):014:0>

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

On 29 November 2011 12:04, Somnath M. [email protected] wrote:

irb(main):003:0> user = User.new(:name => “Michael H.”, :email =>
[email protected]”)
=> #<User id: nil, name: “Michael H.”, email: “[email protected]”,
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…

On Nov 29, 9:04am, Somnath M. [email protected] wrote:

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.


Luis L.

On 29 November 2011 12:04, Somnath M. [email protected] wrote:

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.

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

$ 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:
nil>
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 H.”, :email =>
[email protected]”)
=> #<User id: nil, name: “Michael H.”, email: “[email protected]”,
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(‘[email protected]’) 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”, "[email protected]
"], [“name”, “Michael H.”], [“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 H.”, email: “[email protected]”,
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

On 29 November 2011 12:18, Colin L. [email protected] wrote:

On 29 November 2011 12:04, Somnath M. [email protected] wrote:

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.

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”)

On 29 November 2011 12:30, Somnath M. [email protected] wrote:

irb(main):003:0> user = User.new(:name => “Michael H.”, :email =>
"], [“name”, “Michael H.”], [“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 H.”, email: “[email protected]”,
created_at: “2011-11-29 12:28:21”, updated_at: “2011-11-29 12:28:21”>

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

On 29 November 2011 12:30, Somnath M. [email protected] wrote:

$ rails console --sandbox

ermm… “sandbox”? :wink:

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?

On 29 November 2011 12:47, Somnath M. [email protected] wrote:

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:

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

On 29 November 2011 12:47, Somnath M. [email protected] wrote:

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


Posted via http://www.ruby-forum.com/.


You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


gplus.to/clanlaw

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs