Rails 3.1 Ransack HABTM

妖精的绣舞 提交于 2020-01-02 01:45:07

问题


Is HABTM supported by Ransack?

Having the models:

  • Shop HABTM Categories
  • Category HABTM Shops

Can I use ransack to search a Shop by a single category? What does the form look like?


回答1:


I think the field you're trying to use would be

:categories_id_eq

Usage would be something like this

<%= f.label :categories_id_eq, "Category" %>
<%= f.collection_select :categories_id_eq, Category.order(:title), :id, :title %>



回答2:


You should be aware that there are gotchas: while this workds fine

:categories_id_eq

If you want to find products in any category you can go with

:categories_id_in

But if you need to get products belonging to all of categories selected it wont work as expected:

:categories_id_all

returns zero results see discussions:

Rails, Ransack: How to search HABTM relationship for "all" matches instead of "any"

Convert ActiveRecord habtm query to Arel .



来源:https://stackoverflow.com/questions/11619246/rails-3-1-ransack-habtm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!