2
votes

I am trying to get my user model to work in ActiveAdmin but it seems to only work when I refer the models back to the user model itself, which then breaks my forms in my app.

This way breaks my ActiveAdmin user view but by forms work in my app.

user.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :vehicle
  has_one :permit
  has_one :faculty
  has_one :emergency_contact
  has_one :student

  def admin?
    roles == "admin"
  end

  def editor?
    roles == "editor"
  end

  def standard?
    roles == "standard"
  end

end

user.rb

has_many :vehicle, class_name: 'User'
has_one :permit, class_name: 'User'
has_one :faculty, class_name: 'User'
has_one :emergency_contact, class_name: 'User'
has_one :student, class_name: 'User'

The second way is what gets my ActiveAdmin user view to work (I know this is wrong just don't know how to fix it) but it breaks my forms in my app. When ActiveAdmin is broken I am getting the error: undefined method 'vehicle_id_eq' for Ransack::Search<class: User when I click on the users view in ActiveAdmin.

Anyone have any ideas on what I can do to fix my model to get ActiveAdmin to work properly?

Edit**

admin/user.rb

ActiveAdmin.register User do
    permit_params :roles
end

models/vehicle.rb

class Vehicle < ApplicationRecord
    belongs_to  :user
end
1
Can you add the full stack trace of the error? and the exact request too? - wspurgin
Also add the admin view you've created for the User model if you can. - wspurgin
So the full trace is super long and about 50 lines of redirects. Do you still want all of it? The request is all I'm doing is calling upon the page /admin/users - Bryce Renninger
Fair, how about the top 10 lines? I'm mostly interested in the ActiveAdmin part of the stack trace. Or you can put the whole thing in a paste bin and just link it here. - wspurgin
pastebin.com/JZvpS5rS Forgot about pastebin. - Bryce Renninger

1 Answers

1
votes

ActiveAdmin is trying to create filters for all your associations on the User model. That appears to include your has_many association (and has_ones too I'd guess). This is such a simple case that it seems buggy that AA is trying to create filters for has_one or has_many associations by default. It might be worth reporting on Github. In the mean time, there are a few ways to get around this issue.

  1. Specify your own filters

in admin/user.rb

ActiveAdmin.register User do
    permit_params :roles

    filter :name
    filter :email
    # or you can remove filters with => remove_filter :vehicles
    #... add more filters that as you need.
end

This way you have the filters you'd actually use to find a single or set of users.

  1. You can join or include the models that are on being referenced. I'd only recommend this if you actually plan on filtering Users by any of those associations. If not, use the first method mentioned above.

in admin/user.rb

ActiveAdmin.register User do
    permit_params :roles

    controller do
      active_admin_config.includes.push :vehicles, :permit #,... etc.

      # IF YOU INCLUDE A `has_many`, you need to ensure you are not 
      # returning duplicate resources. So you need to overwrite
      # the apply_filtering method
      def apply_filtering(collection)
        super.where(User.primary_key => collection)
      end
    end
end