Search code examples
ruby-on-railsnomethoderror

Object doesn't support #inspect when used with .include


So I've run into a nasty error that I don't even know how to debug. I'm pretty new to Rails, so this has thoroughly kicked my ass. When I use .includes(:school) on a look up by Student, it returns a nilClass error, but when I look up by any other model and .includes(:school), School performs just fine.

class Student < ActiveRecord::Base
    has_many :relationships, foreign_key: "liker_id", dependent: :destroy  
    has_many :matches, foreign_key: "student_1_id", dependent: :destroy  
    belongs_to :school  
    belongs_to :conference  
    belongs_to :would_you_rather  
    belongs_to :hometown  
    belongs_to :year  
    belongs_to :major  
end

class School < ActiveRecord::Base
      belongs_to :conference
      belongs_to :rival, class: "School", foreign_key: "rival_id"
      has_many :students
end

On the console

d = Student.where(:id => '1').includes(:school)

SQL generated

SELECT "schools".* FROM "schools" WHERE "schools"."id" IN (1)

Error message returned

NoMethodError: undefined method `each' for nil:NilClass
from /.rvm/gems/ruby-2.1.2/gems/activerecord4.1.1/lib/active_record/associations/preloader/association.rb:87:in `block in associated_records_by_owner'
from /.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/associations/preloader/association.rb:86:in `each'
from /.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/associations/preloader/association.rb:86:in `associated_records_by_owner'
from /.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/associations/preloader/singular_association.rb:9:in `preload'
from /.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/associations/preloader/association.rb:20:in `run'
from /.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/associations/preloader.rb:136:in `block (2 levels) in preloaders_for_one'
from /.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/associations/preloader.rb:134:in `each'
from /.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/associations/preloader.rb:134:in `map'
from /.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/associations/preloader.rb:134:in `block in preloaders_for_one'  

Before committing the query with .first and just performing a search with

    d = Student.where(:id => '1').includes(:school)

the console generates

Student Load (0.6ms)  SELECT "students".* FROM "students"  WHERE "students"."id" = 1  
School Load (0.2ms)  SELECT "schools".* FROM "schools"  WHERE "schools"."id" IN (1)
(Object doesn't support #inspect)  
=> 

and returns a totally blank line

I'm sure the error lies in here somewhere, I just have no idea how to tackle it. I'm also new to StackOverflow, so if I missed anything feel free to ask. Any help is appreciated.


Solution

  • In the students table, is the school_id column defined as string? Rails didn't like that until very recently. Two options:

    1. Upgrade to the newest version of Rails (4.1.4 as of now).
    2. Change the column type to integer (or UUID)