Skip to content

Instantly share code, notes, and snippets.

@bmulholland
Created March 13, 2015 00:09
Show Gist options
  • Save bmulholland/1c28662f5f8f42242744 to your computer and use it in GitHub Desktop.
Save bmulholland/1c28662f5f8f42242744 to your computer and use it in GitHub Desktop.
undefined method [] for nil:nilclass
# class 1
class Filter < ActiveRecord::Base
has_many :filter_texts, class_name: "Filter::Text"
def initialize
self.filter_texts
end
end
# class 2
class Filter::Text < ActiveRecord::Base
end
# Test
Filter.new
@bmulholland
Copy link
Author

Error:

  1) Filter converts text criteria to text filter
     Failure/Error: Filter.new
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /Users/bmulholland/.rvm/gems/ruby-2.1.2@contractually/gems/activerecord-4.1.9/lib/active_record/associations.rb:165:in `association_instance_get'
     # /Users/bmulholland/.rvm/gems/ruby-2.1.2@contractually/gems/activerecord-4.1.9/lib/active_record/associations.rb:151:in `association'
     # /Users/bmulholland/.rvm/gems/ruby-2.1.2@contractually/gems/activerecord-4.1.9/lib/active_record/associations/builder/association.rb:110:in `filter_texts'
     # ./app/models/filter.rb:15:in `initialize'
     # /Users/bmulholland/.rvm/gems/ruby-2.1.2@contractually/gems/activerecord-4.1.9/lib/active_record/inheritance.rb:30:in `new'
     # /Users/bmulholland/.rvm/gems/ruby-2.1.2@contractually/gems/activerecord-4.1.9/lib/active_record/inheritance.rb:30:in `new'
     # ./spec/models/filter_spec.rb:5:in `block (2 levels) in <top (required)>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment