You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Ransack 4时ActiveAdmin无法加载关联ActiveStorage的模型问题求助

Ransack 4时ActiveAdmin无法加载关联ActiveStorage的模型问题求助

我现在碰到个头疼的问题,每次在ActiveAdmin里加载User或者Photo模型的时候,都会弹出这个臭名昭著的错误:

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

问题根源是这两个模型都关联了ActiveStorage:比如User模型里写了has_one_attached :avatar,Photo模型更简单,代码就是下面这样:

# app/models/photo.rb
class Photo < ApplicationRecord
  has_one_attached :pic
end

我试过把User模型里的has_one_attached :avatar注释掉,这时候ActiveAdmin就能正常加载User列表了,但这显然不是长久之计,附件功能肯定得保留啊。

之前我已经在ApplicationRecord里加了全局的Ransack配置,本来以为能覆盖所有模型的问题,现在其他模型都正常了,就剩User和Photo不行:

# app/models/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

我甚至还试过在User和Photo模型里手动列出允许的Ransack属性,结果还是没用。有没有过来人能给我支个招,怎么才能让这两个模型在ActiveAdmin里正常加载啊?

备注:内容来源于stack exchange,提问作者mystic cola

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 12:12:59