我用Rails 3.2,我已經得到了我在其中要查找所有符合以下條件的行的數據庫表:或在Rails的ActiveRecord的where子句
一個= TRUE和b = true並且(d = 1),a,b,c,d是列。
我能有這樣的事情:
Route.where(:a => true,
:b => true,
:c => 0..1 OR :d=1
).all
我用Rails 3.2,我已經得到了我在其中要查找所有符合以下條件的行的數據庫表:或在Rails的ActiveRecord的where子句
一個= TRUE和b = true並且(d = 1),a,b,c,d是列。
我能有這樣的事情:
Route.where(:a => true,
:b => true,
:c => 0..1 OR :d=1
).all
我可能是錯的,但我不認爲你可以成爲使用該查詢阿雷爾爲主,其中功能;你需要自己組成數據庫查詢字符串。
假設你正在使用SQLite或Postgres的:
Route.where("a = true and b = true and ((c > 0 and c < 1) or d = 1)").all
我還沒有測試此代碼,但我懷疑這可能會爲你做的工作。請注意,這不太「便攜」代碼;如果你改變你使用的數據庫,查詢可能會中斷。
'where('a =:true and b =:true and((c>:lo and c <:hi)或d =:d',:true => true,:lo => 0,:hi => 1,:d => 1)'會更安全,不同的數據庫對「true」 。甚至'.where(:a => true,:b => true).where('c>:lo和c <:hi)或d =:d',...)'。 –
在Rails 4,你也可以做
Route.where(:a => true,:b => true,:c => [1,2]).all
這會發現其中c爲1或2
是.all是必填項?我認爲這是默認。 – learner
沒有必要,我只是保持它與問題一致。 – user2031423
看看http://stackoverflow.com/questions/3548548/rails3 -combine-scope-with-or- –
看看這個截屏http://railscasts.com/episodes/354-squeel – mohamagdy