收到错误:NoMethodError:nil的未定义方法`fetch_value':尝试在rails控制台中创建新记录时为NilClass

时间:2017-01-03 03:40:44

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

我一直在尝试使用Ruby 2.4从Rails 5.0中的rails控制台添加记录。该模型被称为“AuditionGroup”

当我查找AuditionGroup时,一切都很完美:

2.4.0 :017 > AuditionGroup
 => AuditionGroup(id: integer, audition_date: date, audition_time: time, class: string, created_at: datetime, updated_at: datetime) 

然后当我尝试添加新记录时,我收到以下错误消息:

2.4.0 :018 > newgroup = AuditionGroup.new
NoMethodError: undefined method `fetch_value' for nil:NilClass
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.1/lib/active_record/attribute_methods/read.rb:66:in `_read_attribute'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.1/lib/active_record/attribute_methods/read.rb:36:in `__temp__36c6163737'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.1/lib/active_record/core.rb:313:in `initialize'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.1/lib/active_record/inheritance.rb:65:in `new'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.1/lib/active_record/inheritance.rb:65:in `new'
    from (irb):18
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/railties-5.0.1/lib/rails/commands/console.rb:65:in `start'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/railties-5.0.1/lib/rails/commands/console_helper.rb:9:in `start'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `block in require'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
    from /Users/Nick/Sites/testapp/bin/rails:9:in `<top (required)>'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `load'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `block in load'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `load'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/spring-2.0.0/lib/spring/commands/rails.rb:6:in `call'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/spring-2.0.0/lib/spring/command_wrapper.rb:38:in `call'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/spring-2.0.0/lib/spring/application.rb:191:in `block in serve'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/spring-2.0.0/lib/spring/application.rb:161:in `fork'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/spring-2.0.0/lib/spring/application.rb:161:in `serve'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/spring-2.0.0/lib/spring/application.rb:131:in `block in run'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/spring-2.0.0/lib/spring/application.rb:125:in `loop'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/spring-2.0.0/lib/spring/application.rb:125:in `run'
    from /Users/Nick/.rvm/gems/ruby-2.4.0/gems/spring-2.0.0/lib/spring/application/boot.rb:19:in `<top (required)>'
    from /Users/Nick/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/Nick/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

这是我的AuditionGroup模型

class AuditionGroup < ApplicationRecord
  self.table_name = "auditiongroups"
  has_many :auditions
end

2 个答案:

答案 0 :(得分:1)

问题的根源在于您有一个名为class的列。

谷歌列出&#34;保留&#34; Rails中的单词,class就是其中之一。

答案 1 :(得分:0)

这与this post有关。在这种情况下,将attr_accessor添加到您的班级中已设置的所有属性中。