2013-06-26 123 views
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") 

我有一個過濾器parial:

<%= 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 %> 

當我嘗試查看foosno_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) 

回答

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") 
+0

熱宕,你怎麼我錯過。謝謝@ user16484。 –