In my app/models/active_storage/attachment.rb file I had used this code
class ActiveStorage::Attachment < ApplicationRecord
belongs_to :blob, class_name: "ActiveStorage::Blob
def self.ransackable_attributes(auth_object = nil)
["blob_id", "created_at", "id", "name", "record_id", "record_type"]
end
end
When creating Active Admin with Active Storage, I encountered a search error. To address this, I used defined the ransackable method in my model. In my app/models/user.rb I had used
has_one_attached :profile_image
When I open this link http://127.0.0.1:3000/users/1 it show this error:
unknown keywords: :class_name, :as, :inverse_of
And when I open this link http://127.0.0.1:3000/admin it open successfully
I had used inverse of in my app/models/user.rb file, but it did't work I had go through all my schema file it generate activestorage of correctly.
You shouldn't define or redefine any association in built-in
ActiveStorage::Attachmentclass. And of course you shouldn't change inheritance of this class!Look to the source and you will see that parent class is
ActiveStorage::Record. Andbelongs_to :blobhasautosaveoptionIf you need to allow search with ransack, just reopen this class with ransack method. Define only ransack method, don't change other things
You can use initializer to patch built-in classes
Please note
on_loadhook here, it is from source code above. And it is important moment because you can successfully patch only if needed class is already loadedAlso note that inheritance is different in older rails:
ActiveStorage::Attachment's parent class in 5.2 and 6.0 versions isActiveRecord::Base. And 5.2 doesn't support hooks