ruby-on-railsactiveadminruby-on-rails-7ransack

Unable to load ActiveAdmin models with Ransack 4


I'm getting the infamous:

Ransack needs ActiveStorage::Attachment attributes explicitly allowlisted as searchable. Define a ransackable_attributes class method in your ActiveStorage::Attachment model.

...error. This is because my app/models/user.rb has:

has_one_attached :avatar

So it wants me to declare this for some reason. If I comment that line out... then I can load users in activeadmin just fine. But I'd rather solve the problem.

I also have the same problem with my photo.rb because it has an activestorage file attached as well. Used to be no model in activeadmin would load.

But now I've already added these lines to application_record.rb:

def self.ransackable_associations(auth_object = nil)
    @ransackable_associations ||= reflect_on_all_associations.map { |a| a.name.to_s }
  end

  def self.ransackable_attributes(auth_object = nil)
    @ransackable_attributes ||= column_names + _ransackers.keys + _ransack_aliases.keys + attribute_aliases.keys
  end

So now most of the models work except for my User and Photo models.

Here's the very simple photo model for example:

app/models/photo.rb

class Photo < ApplicationRecord

has_one_attached :pic


end

I've also tried actually listing the attributes for ransack... also no dice.

Any way I can get this up and running?


Solution

  • I actually found the answer in a discussion based on a closed Issue on github:

    https://github.com/activeadmin/activeadmin/discussions/8077

    No one has found a "proper" solution to this... but the workaround everyone seems to be going with is removing activestorage from ransack search. So in my case:

    app/admin/photo.rb

    ActiveAdmin.register Photo do
    
    remove_filter :pic_attachment, :pic_blob
    
    end
    

    Also... if you have more than one attachment. (Don't forget to pluralize "has_many" attachments... otherwise they won't work.)

    app/admin/user.rb

    ActiveAdmin.register User do
       remove_filter :avatar_attachment, :avatar_blob, :cpics_attachments, :cpics_blobs
    end
    

    That's it basically. Ransack will ignore the attachment and blob and you can finally go about your day. (Even though my day is over now and it took me ALL of it to figure this out.)