Rails 5.1 ActiveAdmin error undefined method `klass' for nil:NilClass

Viewed 2696

I am receiving the errors below when I try to view two specific Models through ActiveAdmin's dashboard. Everything I've read points to a possible association issue, but I am not sure how to correct it. I see a lot of people having the same problems but haven't found any solutions to it. Any help would be much appreciated.

NoMethodError in Admin::Listings#index
undefined method `klass' for nil:NilClass

or

NoMethodError in Admin::Buildings#index
undefined method `klass' for nil:NilClass

Models

class Company < ApplicationRecord
  has_many :appointments, through: :listings, dependent: :destroy
  has_many :listings, through: :users, dependent: :destroy
  has_many :buildings, through: :users, dependent: :destroy
  has_many :users, dependent: :destroy
end

class User < ApplicationRecord
  belongs_to :company
  has_many :apointments, through: :listings
  has_many :listings, through: :buildings
  has_many :buildings
end

class Listing < ApplicationRecord
  has_many :companies, through: :users
  has_many :users, through: :buildings
  belongs_to :building
  has_many :appointments
end

class Building < ApplicationRecord
  has_many :companies, through: :users
  belongs_to :user
  has_many :appointments, through: :listings
  has_many :listings, dependent: :destroy
end


class Appointment < ApplicationRecord
  belongs_to :listing
  has_many :companies, through: :listings 
end
1 Answers

I think you have a typo in has_many :apointments, through: :lisings

Try using has_many :appointments, through: :listings

You have a missing p and a missing t

There is no resolution but https://github.com/activeadmin/activeadmin/issues/4470 looks like your issue as well.

One other idea:

On the models that have belong_to and you use that association for a through, try changing it to singular. For example:

class Appointment < ApplicationRecord belongs_to :listing has_many :companies, through: :listing # nb: singular here end

Perhaps that'll help?

Related