Ruby on Rails Active Admin - 显示 HABTM 的重复记录

发布于 2025-01-01 22:57:36 字数 444 浏览 1 评论 0原文

我正在 Active Admin gem 中设计一个基本的文件管理器(资产模型)。每个资产 HABTM 组,反之亦然。

在我的 active_admin 资产资源中,我有一个过滤器,我希望能够在其中 选择多个组进行过滤,因此我添加了:

filter :groups_id, :as => :check_boxes, :collection => proc {Group.all}

所有组均按预期显示为复选框。但是,如果我有 asset_1、asset_2,并且将 group_1 分配给 asset_1 和 asset_2,将 group_2 分配给 asset_2,当我 按两个角色进行过滤,asset_2 将自身列出两次。

如何限制过滤器仅使用要返回的“不同”或“唯一”资产?

我还有另一个问题,那就是过滤器在我的任何范围内根本不起作用。

I am designing a basic file manager (the Asset model) in the Active Admin gem. Each Asset HABTM Groups, and vice-versa.

In my active_admin Asset resource I have a filter where I want to be able to
select multiple groups to filter by, so I added:

filter :groups_id, :as => :check_boxes, :collection => proc {Group.all}

All of the groups show up as checkboxes as expected. However, if I have asset_1, asset_2 and I have group_1 assigned to asset_1 and asset_2, and group_2 to asset_2, when I
filter by both roles, asset_2 lists itself twice.

How can I restrict the filter to use only "distinct" or "unique" assets to be returned?

I also have another problem, which is that the filters are not working at all in any of my scopes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十二 2025-01-08 22:57:36

威尔的答案的快速更新。我正在运行 Rails 5.0 和 ActiveAdmin 1.0,并且 clean_search_params 返回错误。但这反而有效:

def apply_filtering(chain)
  super
  @search.result(distinct: true)
end

谢谢!

A quick update on Will's answer. I'm running Rails 5.0 and ActiveAdmin 1.0, and clean_search_params returned an error. But this worked instead:

def apply_filtering(chain)
  super
  @search.result(distinct: true)
end

Thanks!

酒解孤独 2025-01-08 22:57:36

活动管理员读取指示添加

distinct: true 

以获得独特的结果。

要将其应用于活动管理,我正在使用这样做:

controller do
  def apply_filtering(chain)
     @search = chain.ransack clean_search_params params[:q]
     @search.result(distinct: true)
   end
end

Active admin read indicates to add

distinct: true 

to get unique results.

To apply that to active admin, I'm using doing that like this:

controller do
  def apply_filtering(chain)
     @search = chain.ransack clean_search_params params[:q]
     @search.result(distinct: true)
   end
end
剑心龙吟 2025-01-08 22:57:36

has_and_belongs_to_many 接受 :uniq 选项,确保仅返回 uniq 记录。在你的模型中设置这个应该可以解决问题。

class MyModel
  has_and_belongs_to_many :things, :uniq => true
end

has_and_belongs_to_many accepts a :uniq option which ensures that only uniq records will be returned. Setting this in your model should do the trick.

class MyModel
  has_and_belongs_to_many :things, :uniq => true
end
嘿咻 2025-01-08 22:57:36

...并且,快速补充 Alex 的答案:

如果您想对应用程序中的所有控制器执行此操作,您可以将其添加到初始化程序(我的称为 active_admin_patches.rb) -

# This guarantees that the results for a filtered #index page search do not appear more than once, on any #index page in the AA app
# TODO: THIS WILL PROBABLY FAIL WITH SOME FUTURE UPDATE, SO BE READY TO UPDATE IT FROM THE LATEST GEM SOURCE
module ActiveAdmin::ResourceController::DataAccess
  # Applies any Ransack search methods to the currently scoped collection.
  # Both `search` and `ransack` are provided, but we use `ransack` to prevent conflicts.
  def apply_filtering(chain)
    @search = chain.ransack(params[:q] || {})
    # This is the original line
    # @search.result
    # This is the patch
    @search.result(distinct: true)
  end
end

我不是肯定为什么有人希望这是默认行为,但可能是有原因的。嗯,也许对于索引视图的列是非不同行之一的情况。是的,一定是这样。

另外,肯定有一种更好的方法来修复这个问题,但我很着急。 :-)

... and, a quick addition Alex's answer:

If you want to do this for all controllers in your app, you can add this to an initializer (mine's called active_admin_patches.rb) -

# This guarantees that the results for a filtered #index page search do not appear more than once, on any #index page in the AA app
# TODO: THIS WILL PROBABLY FAIL WITH SOME FUTURE UPDATE, SO BE READY TO UPDATE IT FROM THE LATEST GEM SOURCE
module ActiveAdmin::ResourceController::DataAccess
  # Applies any Ransack search methods to the currently scoped collection.
  # Both `search` and `ransack` are provided, but we use `ransack` to prevent conflicts.
  def apply_filtering(chain)
    @search = chain.ransack(params[:q] || {})
    # This is the original line
    # @search.result
    # This is the patch
    @search.result(distinct: true)
  end
end

I'm not sure why anybody wouldn't want this to be the default behavior, but there's probably a reason. Hmm, maybe for cases where a column of the index view is one of the non-distinct rows. Yeah, that must be it.

Also, there's bound to be a better way to patch this less intrusively, but I'm in a hurry. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文