【发布时间】:2016-01-10 07:51:25
【问题描述】:
我正在尝试使用名为 Genes 的过滤器搜索 Item 模型,该过滤器由 has_many through 连接。
我的问题是当我选择两种或更多类型时,它也会选择只包含一种类型的记录。
示例:有两种类型,名为动作和冒险。记录 A 具有动作类型,记录 B 具有两种类型。通过在我的视图中选中两个流派复选框,Rails 返回的记录包括记录 A 和 B,而它应该只是记录 B。
这是我在控制器下的过滤语句
//This is under controller
@item = Item.includes(:genres).where('genres.id' => params['genre_ids'])
// This is under my view
<%= simple_form_for Item.new, :method => :get, url: search_path do |f| %>
<% Genre.all.each do |sc| %>
<div>
<label>
<%= check_box_tag 'genre_ids[]',sc.id %>
<span style="font-weight:normal;"><%= sc.name %> </span>
</label>
</div>
<% end %>
<% end %>
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 activerecord has-many-through