Ruby on Rails Active Admin - 显示 HABTM 的重复记录
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
威尔的答案的快速更新。我正在运行 Rails 5.0 和 ActiveAdmin 1.0,并且
clean_search_params
返回错误。但这反而有效:谢谢!
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:Thanks!
活动管理员读取指示添加
以获得独特的结果。
要将其应用于活动管理,我正在使用这样做:
Active admin read indicates to add
to get unique results.
To apply that to active admin, I'm using doing that like this:
has_and_belongs_to_many 接受
:uniq
选项,确保仅返回 uniq 记录。在你的模型中设置这个应该可以解决问题。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....并且,快速补充 Alex 的答案:
如果您想对应用程序中的所有控制器执行此操作,您可以将其添加到初始化程序(我的称为
active_admin_patches.rb
) -我不是肯定为什么有人不希望这是默认行为,但可能是有原因的。嗯,也许对于索引视图的列是非不同行之一的情况。是的,一定是这样。
另外,肯定有一种更好的方法来修复这个问题,但我很着急。 :-)
... 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
) -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. :-)