0

I've got this stupid thing... I'm sure I just miss something obvious but yahoogling didn't solve the problem.

All I do is

rails new TestApp

and

cd TestApp
rails generate scaffold User name:string age:integer
bundle exec rake db:create
bundle exec db:migrate

which works fine.

But when I go to the IRB, there is no User!

u = User.first
    NameError: uninitialized constant User
    from (irb):3
    from /usr/bin/irb:12:in `<main>'

What's wrong here?

Cheers

tshepang
  • 12,111
  • 21
  • 91
  • 136
EasierSaidThanDone
  • 1,877
  • 4
  • 20
  • 29

2 Answers2

8

Don't use irb, instead:

rails console

which will have every model of your project imported.

davids
  • 6,259
  • 3
  • 29
  • 50
  • I'd suggest you add the User.create bit to your answer since it is also a problem in the OPs code. That way, others with this problem will immediately see the whole answer instead of just part of it. – vlasits May 24 '12 at 14:13
  • Thanks for the suggestion @vlasits, but I think the problem of the question is just that the model User and every rails dependency is not being loaded, so irb will rise an exception if you try to use it. If you call User.first in rails console, you won't get any error, just an empty list (or a null value, don't remember now) – davids May 24 '12 at 14:17
  • Yes. You're right, the reason for the error is the rails app isn't loaded, but the OP expected that User.first would produce a non-empty response ("But when I go to the IRB, there is no User!'). Anyway, suit yourself, yours *is* the correct answer. – vlasits May 24 '12 at 14:28
  • there is no User (i.e. the Class, not the instance) – EasierSaidThanDone May 26 '12 at 11:49
2

You haven't created a user and you are using plain old irb instead of rails console.

open a rails console and try:

User.create(:name => "Jimmy", :age => 14)

Then try

u = User.first
vlasits
  • 2,215
  • 1
  • 15
  • 27
  • Yeah, @davids deserved it anyway since my first idea was the User.create one and only after I saw his answer did I add the rails console bit in. :) You needed both to get happy, but his was the more important suggestion. – vlasits May 24 '12 at 14:12