2014-11-22 46 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