Unable to load ActiveAdmin models with Ransack 4

204 Views Asked by At

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?

2

There are 2 best solutions below

0
mystic cola On BEST ANSWER

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.)

0
Muhammad Ateq Ejaz On

Following steps helped me to fix this issue

Step 1:

  In your rails project root directory create a folder named 'active_storage'

Step 2:

  Inside above folder Create a new file and name it as 'ransackable_attachment.rb'

Step 3: Add following code in it (Add inside ransackable_attachment.rb)

   module RansackableAttachment
      def ransackable_attributes(_auth_object = nil)
          %w[blob_id created_at id id_value name record_id record_type]
      end
    end

   ActiveSupport.on_load(:active_storage_attachment) do
     ActiveStorage::Attachment.extend RansackableAttachment
   end 

Step 4: Load this File in config/application.rb

module MyProject
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 7.0

    Rails.configuration.to_prepare do
      require_relative '../active_storage/ransackable_attachment'
    end
  end
end

Special Thanks https://github.com/activeadmin/activeadmin/discussions/8077, https://omarish.com/active-storage-ransack-error