0

I created a scaffold in Rails 3 and ran the migration. But when I tried to use the rails console to create an instance of the model, it doesn't work. I repeated the same process in Rails 4 and it works perfectly. Here is the commands I typed in.

rails new testApp rails g scaffold Test name:string rake db:migrate rails console p = Test.new

and below is the response I got: 1.9.3p194 :002 > p = Test.new NoMethodError: undefined method new' for Test:Module from (irb):2 from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.15/lib/rails/commands/console.rb:47:instart' from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.15/lib/rails/commands/console.rb:8:in start' from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.15/lib/rails/commands.rb:41:in' from script/rails:6:in require' from script/rails:6:in'

Please help. I am a beginner in rails. Thank you.

timchunght
  • 292
  • 2
  • 3

2 Answers2

2

Test is a module of Unit testing. When you type it in console, this constant get shown instead of your own model.

The only lesson is to avoid using sensitive names in your app's constant names.

Billy Chan
  • 24,625
  • 4
  • 52
  • 68
  • Thank you. I will try again with a different name. – timchunght Nov 17 '13 at 21:41
  • Yes, changing the name to Status actually works on rails 3. I have another question. Why does Test work in rails 4 and not rails 3. I created a test model in rails 4, and it works on rails console. Thank you. – timchunght Nov 17 '13 at 21:47
  • In rails 4, you probably just got lucky. It's related to the class loader and order of operations. If, by some means, your Test model class get's loaded first, then it'll win. Rails4 may have changed the class loading order which might make that poorly named class, `Test` load before the Unit test version which could conflict. – mr rogers Jan 20 '14 at 08:08
0

For a full list of reserved keywords check out: http://reservedwords.herokuapp.com/

Erwin Schens
  • 424
  • 2
  • 9