2014-11-22 45 views
0

我對rails很新穎。範圍使用幾個查詢的模型,Rails 4

我有一個有參與者和參與者類別的應用程序。我爲參與者分類爲「1」或分類「8」的參與者創建了一個命名範圍。我想要一個/或類型的查詢。這看起來很基本......但我最難找到SO上的答案。

這裏就是我現在:

scope :organizer, -> { where(cat_id: 1 or cat_id: 8)} 

回答

3

試試這個:

scope :organizer, -> { where('cat_id = ? or cat_id = ?', 1, 8) } 

什麼錯在這裏:

您嘗試混合SQL操作OR和Ruby方法調用。

爲今後的研究測試查詢在rails console

=> User.where(first_name: 'Foo').to_sql 
#> "SELECT \"users\".* FROM \"users\" WHERE \"users\".\"first_name\" = 'Foo'" 
=> User.where(first_name: 'Foo' or last_name: 'faw').to_sql 
SyntaxError: unexpected keyword_or, expecting ')' 
User.where(first_name: 'Foo' or last_name: 'faw').to_sql 
         ^
+0

是的!我已經看到了這種查詢,現在我確切知道它的用途。謝謝。儘快接受。 – NothingToSeeHere 2014-11-22 18:49:33

+0

很好的解釋..真正感激.. + 1 – 2014-11-22 19:22:53