I have been working in many Rails applications and during the period I have found many bugs but one thing really pissed me off is the error showing the Ruby file name in the terminal.
I tried to run
User.find(3)
in the Rails console, and, though I don't have more than one user record, it was obviously showing an error and I was aware of it. But the location part really messed up my terminal.
Here is the error log:
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=3
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.0.beta2/lib/active_record/core.rb:150:in `find'
from (irb):79
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:110:in `start'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:9:in `start'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands.rb:17:in `<top (required)>'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `block in require'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /home/travis/tumpy/bin/rails:8:in `<top (required)>'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `block in load'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /home/travis/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/travis/.rvm/rubies/ruby-
The solution I was expecting is to hide the location part and only display the error, showing
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=3
IRB has a back_trace_limit option which defaults to 16:
irb(main):001:0> IRB.CurrentContext.back_trace_limit
#=> 16
Setting it to 0 suppresses the entire backtrace:
irb(main):002:0> IRB.CurrentContext.back_trace_limit = 0
#=> 0
irb(main):003:0> User.find(0)
# User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 0 LIMIT 1
#ActiveRecord::RecordNotFound: Couldn't find User with id=0
irb(main):004:0>
You can also pass this as a command line option:
$ rails console -- --back-trace-limit=0
Related
I am adding the elasticsearch gem to an existing rails app hosted on Heroku.
Running MyModel.import in production is raising an error
MultiJson::ParseError: unexpected character at line 1, column 1 [parse.c:690]
As my development and test environments are working perfectly, I assume that this is being caused by the data in MyModel, perhaps an encoding issue. But as this table is quite large, I am having trouble tracking down the problem.
Can anyone confirm that this error is likely to be caused by an
issue in the data being imported?
Is there a way to inspect the JSON data representation that MyModel.import invokes?
EDIT
Following suggestions below, I have tried running a few commands to track down the error.
MyModel.all.each(&:as_json)
MyModel.all.each(&:as_indexed_json)
MyModel.all.each { |r| r.__elasticsearch__.as_indexed_json }
MyModel.all.each { |r| MultiJson.load r.to_json }
are all running without errors. However they do not appear to be returning json, but arrays of records. Is this usual?
[#<MyModel id: 1, ......
Running import on a limited number of records is also causing strange results.
# a single record returns no error
MyModel.import query: -> { where(id: 1) }
=> 0
# multiple records return the error
MyModel.import query: -> { where(id: 1..2) }
MultiJson::ParseError: unexpected character at line 1, column 1 [parse.c:690]
The full stack trace is
MultiJson::ParseError: unexpected character at line 1, column 1 [parse.c:690]
from /app/vendor/bundle/ruby/2.4.0/gems/multi_json-1.11.3/lib/multi_json/adapters/oj.rb:15:in `load'
from /app/vendor/bundle/ruby/2.4.0/gems/multi_json-1.11.3/lib/multi_json/adapters/oj.rb:15:in `load'
from /app/vendor/bundle/ruby/2.4.0/gems/multi_json-1.11.3/lib/multi_json/adapter.rb:20:in `load'
from /app/vendor/bundle/ruby/2.4.0/gems/multi_json-1.11.3/lib/multi_json.rb:119:in `load'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-transport-5.0.4/lib/elasticsearch/transport/transport/serializer/multi_json.rb:24:in `load'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-transport-5.0.4/lib/elasticsearch/transport/transport/base.rb:322:in `perform_request'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-transport-5.0.4/lib/elasticsearch/transport/transport/http/faraday.rb:20:in `perform_request'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-transport-5.0.4/lib/elasticsearch/transport/client.rb:131:in `perform_request'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-api-5.0.4/lib/elasticsearch/api/actions/bulk.rb:95:in `bulk'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-model-5.0.1/lib/elasticsearch/model/importing.rb:123:in `block in import'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-model-5.0.1/lib/elasticsearch/model/adapters/active_record.rb:106:in `block in __find_in_batches'
from /app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.0.6/lib/active_record/relation/batches.rb:121:in `block in find_in_batches'
from /app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.0.6/lib/active_record/relation/batches.rb:214:in `block in in_batches'
from /app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.0.6/lib/active_record/relation/batches.rb:198:in `loop'
from /app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.0.6/lib/active_record/relation/batches.rb:198:in `in_batches'
from /app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.0.6/lib/active_record/relation/batches.rb:120:in `find_in_batches'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-model-5.0.1/lib/elasticsearch/model/adapters/active_record.rb:105:in `__find_in_batches'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-model-5.0.1/lib/elasticsearch/model/importing.rb:122:in `import'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-model-5.0.1/lib/elasticsearch/model.rb:118:in `import'
from (irb):14
from /app/vendor/bundle/ruby/2.4.0/gems/railties-5.0.6/lib/rails/commands/console.rb:65:in `start'
from /app/vendor/bundle/ruby/2.4.0/gems/railties-5.0.6/lib/rails/commands/console_helper.rb:9:in `start'
from /app/vendor/bundle/ruby/2.4.0/gems/railties-5.0.6/lib/rails/commands/commands_tasks.rb:78:in `console'
from /app/vendor/bundle/ruby/2.4.0/gems/railties-5.0.6/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /app/vendor/bundle/ruby/2.4.0/gems/railties-5.0.6/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:9:in `require'
from bin/rails:9:in `<main>'
This is proving to be a frustrating issue to debug. I am grateful for any further suggestions or ideas.
when i run a method in terminal of rails.
first time, it working :
Spree::Campaign.first
Campaign Load (0.4ms) SELECT `campaigns`.* FROM `campaigns` ORDER BY `campaigns`.`id` ASC LIMIT 1
=> #<Campaign id: 1, name: "campaign 1", user_id: 1, created_at: "2015-10-27 06:48:01", updated_at: "2015-10-29 04:22:03", description: nil, active: true>
but when i try run code above again
Spree::Campaign.first
NameError: uninitialized constant Spree::Campaign
from (irb):2
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/railties-4.1.6/lib/rails/commands/console.rb:90:in `start'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/railties-4.1.6/lib/rails/commands/console.rb:9:in `start'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/railties-4.1.6/lib/rails/commands.rb:17:in `<top (required)>'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
from /home/kop/rails/donghoxteen/bin/rails:8:in `<top (required)>'
from /home/kop/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/kop/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
Why? and how to fix this error??
The errors you are getting seem to be intermittent. In your question text, the error is uninitialized constant Spree::Campaign, whereas in your question title, the error is uninitialized constant Class.
This points to there being something fundamentally wrong with your Ruby installation. Class is a core Ruby class, and the Class constant should always be there. It's hard to diagnose with just the information you have given, but it appears that constant lookup is broken in your Ruby installation.
I recommend re-installing Ruby.
I have a rails model that keeps track of other models in an app. (unusual, but necessary for this project).
When I try to assign the attribute model_name I get an error that occurs once, but then doesn't occur again.
AppModel
model_name created_at updated_at
----------------------------------------
User
Question
Post
Answer
In console, I'm getting a strange error:
> model = AppModel.new
> model.model_name = "User" # First time causes the error.
> model.model_name = "User" # Second time it works.
Error:
ActiveRecord::DangerousAttributeError: record_changed? is defined by Active Record
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:104:in `instance_method_already_implemented?'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:288:in `block in define_attribute_method'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:285:in `each'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:285:in `define_attribute_method'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:252:in `block in define_attribute_methods'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:252:in `each'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:252:in `define_attribute_methods'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:75:in `block in define_attribute_methods'
from /Users/donaldpinkus/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mutex_m.rb:73:in `synchronize'
from /Users/donaldpinkus/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mutex_m.rb:73:in `mu_synchronize'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:72:in `define_attribute_methods'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:196:in `method_missing'
from (irb):3
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands/console.rb:90:in `start'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands/console.rb:9:in `start'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:69:in `console'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `block in require'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
from /Users/donaldpinkus/Projects/hooker/bin/rails:8:in `<top (required)>'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `load'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `block in load'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `load'
from /Users/donaldpinkus/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/donaldpinkus/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'2.1.5 :004 > e.model_name = "User"
=> "User"
"model_name" isn't a reserved word for models, so what is happening here?
This happens because there is already a method in ActiveRecord models, which is called exactly model_name, that returns (obviously) the name of this model (among other things).
From:
.../ruby-2.1.5/gems/activemodel-4.2.0/lib/active_model/naming.rb
# line 222:
Owner: ActiveModel::Naming
Visibility: public
Signature: model_name()
Number of lines: 12
Returns an ActiveModel::Name object for module. It can be used to
retrieve all kinds of naming-related information (See
ActiveModel::Name for more information).
When ActiveRecord fetches an object from the table, it also creates methods with names corresponding to the column names in the table. What happens is it fetches an object, sees a column model_name on the given object, tries to define a method model_name and suddenly finds out that it already exists.
Here's a thing: avoid needlessly long attribute names. If you're defining a name in a model, it can just be name, it's scoped to "model" class anyway, obviously it's the model's name.
The error reporting code, however, likely contains a bug that needs to be tracked down.
I have a question with Ruby on Rails using:
rails console --sandbox
So in Michael Hartl's rails tutorial 3rd edition we are using this and I have a problem when sometimes I'm using it and it all goes well and then I have an error, and literally the whole test environment becomes useless in that it will not respond to my commands
For example, I will type:
2.1.1 :025 > user.first
or any other command that was working and should be working and instead get:
NoMethodError: undefined method `first' for #<User:0x007fede35e3188>
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activemodel-4.2.0.beta2/lib/active_model/attribute_methods.rb:435:in `method_missing' from (irb):25
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2 gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:110:in `start'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2 gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:9:in `start'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/railties-4.2.0.beta2/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `block in require'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /Users/andrewkim/workspace2/sample_app/bin/rails:8:in `<top (required)>'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `block in load'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/commands/rails.rb:6:in `call'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/command_wrapper.rb:38:in `call'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:180:in `block in serve'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:153:in `fork'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:153:in `serve'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/andrewkim/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/andrewkim/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'2.1.1 :026 >
All of a sudden anything I type in terminal will come with an error like this, and I don't know how to go back without exiting "rails console --sandbox". is there a way?
for example, I will type:
2.1.1 :025 > user.first
or any other command that was working and should be working
That command shouldn't work, and it doesn't work. You are getting an error because in the tutorial a User instance, which represents one row in your table, e.g.
user = User.new(name: "Joe", email: "joe#yahoo.com")
does not have a method named first(). Instead, a user instance has the methods:
id()
name()
email()
created_at()
updated_at()
However, the class name(or model name) can be used to search the table, e.g.
user1 = User.first. #User is the name of the class/model
As for this:
and I don't know how to go back
Go back to what? Nothing has changed. All the variables you typed into the console before that error will still exist. Here is an example from the rails console:
$ rails console --sandbox
Loading development environment in sandbox (Rails 4.0.8)
Any modifications you make will be rolled back on exit
2.0.0-p481 :001 > x = 10
=> 10
2.0.0-p481 :002 > user = User.find_by(email: "example#railstutorial.org")
User Load (4.0ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'example#railstutorial.org' LIMIT 1
=> #<User id: 2, name: "Example User", email: "example#railstutorial.org", created_at: "2014-10-02 01:39:04", updated_at: "2014-10-02 01:39:04", password_digest: "$2a$10$VNzivk5opu1AC5DOM467dO2JaTg3c3JC9OAXR0AqvEi0...">
2.0.0-p481 :003 > user.first
NoMethodError: undefined method `first' for #<User:0x00000100f360c8>
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481#sample_app2_gems/gems/activemodel-4.0.8/lib/active_model/attribute_methods.rb:439:in `method_missing'
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481#sample_app2_gems/gems/activerecord-4.0.8/lib/active_record/attribute_methods.rb:168:in `method_missing'
from (irb):3
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481#sample_app2_gems/gems/railties-4.0.8/lib/rails/commands/console.rb:90:in `start'
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481#sample_app2_gems/gems/railties-4.0.8/lib/rails/commands/console.rb:9:in `start'
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481#sample_app2_gems/gems/railties-4.0.8/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.0.0-p481 :004 > user
=> #<User id: 2, name: "Example User", email: "example#railstutorial.org", created_at: "2014-10-02 01:39:04", updated_at: "2014-10-02 01:39:04", password_digest: "$2a$10$VNzivk5opu1AC5DOM467dO2JaTg3c3JC9OAXR0AqvEi0...">
2.0.0-p481 :005 > x
=> 10
2.0.0-p481 :006 >
See? Everything is still there. On the other hand, if by "go back" you mean, "go back to the blissful state you were in when you got no errors", then the solution is to type the correct commands into the console. Next time, copy the command from the tutorial that you are trying to emulate and paste it into a blank text file, then copy the command that is giving you an error(but which you know is correct!) and paste it under the command from the tutorial:
User.first
user.first
Then compare them. Your problem can also be mitigated somewhat by never creating a variable that has the same name as your model, e.g. instead of writing:
user = User.new(...)
write:
my_user = User.new(...)
Then if you write:
user.first
You will get the error:
NameError: undefined local variable or method `user' for main:Object
which should be easier for you to debug.
Trying to learn the Ruby on Rails 3 Tutorial book, hung up right now: Section 2.2
example says:
rails generate scaffold User name:string email:string
I get:
C:\Sites\rails_projects\demo_app>rails generate scaffold User name:string email:
string
C:/RailsInstaller/Ruby1.9.2/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:
34:in `require': wrong number of arguments (0 for 1) (ArgumentError)
from C:/Sites/rails_projects/demo_app/Gemfile:4:in `evaluate'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/bundler-1.0.21
/lib/bundler/dsl.rb:7:in `instance_eval'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/bundler-1.0.21
/lib/bundler/dsl.rb:7:in `evaluate'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/bundler-1.0.21
/lib/bundler/definition.rb:17:in `build'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/bundler-1.0.21
/lib/bundler.rb:138:in `definition'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/bundler-1.0.21
/lib/bundler.rb:126:in `load'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/bundler-1.0.21
/lib/bundler.rb:110:in `setup'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/bundler-1.0.21
/lib/bundler/setup.rb:7:in `<top (required)>'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/site_ruby/1.9.1/rubygems/custo
m_require.rb:57:in `require'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/site_ruby/1.9.1/rubygems/custo
m_require.rb:57:in `rescue in require'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/site_ruby/1.9.1/rubygems/custo
m_require.rb:35:in `require'
from C:/Sites/rails_projects/demo_app/config/boot.rb:6:in `<top (require
d)>'
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from script/rails:5:in `<main>'
Can someone please set me on the right track? Thanks!
i dont know if you are a new comer to rails 3 but if not and also if you are, i think DEVISE is a good gem to use .i have finished the book and i used devise for my authentication system. all you need do is install it manually from github so as to read its documentation and also more tutorials on ASCiiCast and railscast for videos (engaging with devise). it as a lot of methods that is used in the book e.g if signed_in, current_user, logged_in e.t.c and also destroy the user model before using DEVISE.
I would review your Gemfile to see that you have everything entered correctly.
HTH