【问题标题】:Get all associated records after filtering过滤后获取所有关联记录
【发布时间】:2021-05-24 09:51:57
【问题描述】:

我用过滤器撞墙了。

我有两个模型和相关的 rails_admin conf:

class ImageBase < ApplicationRecord
  has_and_belongs_to_many :authors, optional: true
  [...]
end

class Author < ApplicationRecord
  has_and_belongs_to_many :image_bases, class_name: 'ImageBase'
  [...]
end

config.model 'ImageBase' do
    list do
      scopes [:published, :draft, nil]
      filters %i[collocazione authors]
      [...]
      field :authors do # (4)
        eager_load true
        queryable true
        visible true
        searchable %i[name surname]
      end
      search_by :search_image_base_admin
    end
end

我的问题是,当我在 Rails_admin 中按作者过滤 ImageBase 时,结果只显示匹配的作者,而不是与图像库记录相关的所有作者。

我主要需要这个来导出过滤后的数据,这不仅仅是列表视图中的问题(我可以忍受)

在文档和网络上都找不到解决方案, 有什么建议/解决方案吗?

谢谢 弗朗切斯科

【问题讨论】:

  • 我不明白你的问题,当你说“to the imagebase record”时,你是什么意思?您正在配置图像库记录列表,因此如果您按单个演员过滤,您将仅获得与该特定演员相关的图像库记录。你能重新提出你的问题吗?
  • 我的意思是:当我在 rails_admin 中按作者(关系 m2m)过滤 ImageBase(我的主表)时,结果只显示我在 ImageBase 结果中过滤的同一作者,而不是所有作者与记录有关。示例:ImageBaseRecord1 <23>

标签: ruby-on-rails rails-admin


【解决方案1】:

我不确定我是否理解,但您始终可以覆盖内置搜索并使用范围构建您自己的搜索,就像这样

  RailsAdmin.config do |config|
    config.model ImageBase do
      list do
        search_by :custom_search
      end
    end
  end

scope :custom_search, ->(query) { where(name: query.reverse) }

【讨论】:

  • 谢谢,但我的问题出在过滤器部分,而不是搜索本身。过滤器在范围之前由 rails_admin 处理。
  • 你总是可以做 unscope.where(something) 但你也会失去分页
猜你喜欢
  • 2015-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-10
  • 1970-01-01
  • 2013-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多