Active Record NameError

时间:2014-11-17 21:02:53

标签: ruby-on-rails ruby rails-activerecord

运行以下命令时出错,我无法弄清楚原因。我正在浏览这个在线教程,所有内容对我来说都是相同的,直到16.55分钟video

我正在尝试自己的东西,它没有用,所以我想我会从头开始重复多次。

richie.car.create(:year => 2012,:condition =>'awesome')

NameError: undefined local variable or method `richie' for main:Object
from (irb):5
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/railties-4.1.7/lib/rails/commands/console.rb:90:in `start'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/railties-4.1.7/lib/rails/commands/console.rb:9:in `start'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/railties-4.1.7/lib/rails/commands/commands_tasks.rb:69:in `console'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/railties-4.1.7/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/railties-4.1.7/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:247:in `require'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:247:in `block in require'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:247:in `require'
from /Users/kylekozma/testapp/bin/rails:8:in `<top (required)>'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `load'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `block in load'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `load'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/spring-1.1.3/lib/spring/commands/rails.rb:6:in `call'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/spring-1.1.3/lib/spring/command_wrapper.rb:38:in `call'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/spring-1.1.3/lib/spring/application.rb:180:in `block in serve'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/spring-1.1.3/lib/spring/application.rb:153:in `fork'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/spring-1.1.3/lib/spring/application.rb:153:in `serve'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
from /Users/kylekozma/.rvm/gems/ruby-2.1.4/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/kylekozma/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/kylekozma/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'

但是,当我使用以下代码进行测试时,它正在运行。但是,视频中没有显示以下内容,但显示给我。

 ActiveRecord::Associations::CollectionProxy []

 User.last.cars
 User Load (0.2ms)  SELECT  "users".* FROM "users"   ORDER BY "users"."id" DESC LIMIT 1
  Car Load (0.2ms)  SELECT "cars".* FROM "cars"  WHERE "cars"."user_id" = ?  [["user_id",        2]]
   => #<ActiveRecord::Associations::CollectionProxy []> 
 2.1.4 :010 > 

0 个答案:

没有答案