2

I have been trying to add a record from the rails console in Rails 5.0 with Ruby 2.4. The model is called "AuditionGroup"

When I lookup AuditionGroup everything works perfectly:

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

Then when I try to add a new record I get the following error message:

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>'

This is my AuditionGroup model

class AuditionGroup < ApplicationRecord
  self.table_name = "auditiongroups"
  has_many :auditions
end
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145

2 Answers2

3

The root of the problem is that you have a column called class.

Google for a list of "reserved" words in Rails, class is one of these.

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
0

This is related to this post. In this case add attr_accessor to your class to all attributes that were being set.

Fishy
  • 1,275
  • 1
  • 12
  • 26