Search code examples
ruby-on-railsactiveadminhas-and-belongs-to-many

ActiveAdmin has_many through - undefined method `klass' for nil:NilClass


Trying to figure out why ActiveAdmin is breaking on me, your help is appreciated.

EDIT: Turns out I ran myself into a bit of an x-y problem situation with this. I apologize to the SO community...

I assumed the has_many :venue_rps relationship was the culprit, but I found out later that it was another issue. Please look below for an updated version of this problem:

My models:

class Venue < ActiveRecord::Base
  belongs_to :nightclub
  has_one :nightclub_boss, through: :nightclub

  belongs_to :rp_boss

  has_many :memberships
  has_many :rps, through: :memberships

  belongs_to :captain

  has_many :reservations
  has_many :payments, through: :reservations

  has_many :cutoffs
  has_many :cutoff_payments, through: :cutoff

  has_many :favorites

  has_many :schedules
end

class Membership < ActiveRecord::Base
  belongs_to :venue, dependent: :destroy
  belongs_to :rp, dependent: :destroy
end

class Rp < User
  has_many :memberships
  has_many :venues, through: :memberships

  has_many :nightclubs, through: :venues
  has_many :nightclub_bosses, through: :nightclubs

  has_many :rp_bosses, through: :venues
  has_many :captains, through: :venues

  has_many :reservations
  has_many :payments, through: :reservations

end

EDIT: Read below for my previous explanation, otherwise I fixed this by doing the following:

  • I changed the join table model from venue_rp to membership. This is my migration:

    class CreateMemberships < ActiveRecord::Migration
      def change
        create_join_table :rps, :venues, table_name: :memberships do |t|
          t.index [:rp_id, :venue_id]
          t.index [:venue_id, :rp_id]
        end
      end
    end
    
  • Since that didn't work, I poked around and ran a debugger to find what was wrong. I found that I botched up the relationships with the cutoff in Venues:

I had:

has_many :cutoffs
has_many :cutoff_payments, through: :cutoff

Needed to make it plural:

has_many :cutoffs
has_many :cutoff_payments, through: :cutoff*s*

After that the exception was gone.

In case you run into another issue in activeadmin such as:

undefined method `memberships_id_eq' for Ransack::Search<class: Venue, base: Grouping <combinator: and>>:Ransack::Search

Just remove the ransack filter for the membership has_many through: model. For this use case it's not necessary to filter my information by that and thus I just removed it.

[OLD] Explanation: I need to assign many Rps to a certain Venue, and many venues to a certain Rp, so I used a join model (venue_rp) backed by a join table to model this information.

So far so good, it works like a charm on the rails console.

However I need to provide administrator users with forms to CRUD data so I went the ActiveAdmin way. So I go ahead and register my Venue model:

ActiveAdmin.register Venue do

  scope_to :current_user, if: proc{ current_user.is_nightclubboss? }
  menu priority: 3

end

The idea being that superadmins will be able to see all the info in the app, and nightclub bosses will only see information that concerns them.

So far so good, I'm expecting that I'll see the default index and forms for activeadmin in venues (Sucursales means Venues in the following screenshot):

enter image description here

But when I click on the Venues entry in the activeadmin menu I run into the reflection issue:

enter image description here

What I've tried is to rewrite the associations, no dice so far. Am I doing something wrong with them?


Solution

  • I ran into an x-y problem kind of situation with this.

    The problem was unrelated to the question.