0

我有一个使用多个过滤器的页面,除了一个过滤器外一切正常。不起作用的过滤器需要检查哪些foos没有任何bars. 也就是说,没有bar记录会与我希望显示的foo_id匹配。foos

对于这个例子,让我们做一个过滤roomfoosno_bars

我在范围(foo.rb)中有类似的东西:

  scope :not_validated, where("mark_validated = false")
  scope :not_located, where("grid_location_id IS NULL")
  scope :no_bars, joins("LEFT JOIN bars ON foos.id = bars.foo_id where").where("bars.id IS NULL")

  scope :room_1, where("room_id = 1")
  scope :room_2, where("room_id = 2")

我有一个过滤器:

<%= dropdown_tag filter_button_name("Room", "room") do %>
    <li><%= multi_filter_link "room", "room_1"%></li>
    <li><%= multi_filter_link "room", "room_2" %></li>
<% end %>

<%= dropdown_tag filter_button_name("Filter By", "filter") do %>
    <li><%= multi_filter_link "filter", "not_validated" %></li>
    <li><%= multi_filter_link "filter", "not_located" %></li>
    <li><%= multi_filter_link "filter", "no_bars" %></li>
<% end %>

当我尝试foos使用no_bars过滤器查看时,我收到以下错误:

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE (bars.id IS NULL)' at line 1: SELECT `foos.*` FROM `foos` LEFT JOIN bars ON foos.id = bars.foo_id where WHERE (bars.id IS NULL)  
4

1 回答 1

1
scope :no_bars, joins("LEFT JOIN bars ON foos.id = bars.foo_id where").where("bars.id IS NULL")

我想你想要:

scope :no_bars, joins("LEFT JOIN bars ON foos.id = bars.foo_id").where("bars.id IS NULL")
于 2013-06-26T17:36:31.453 回答