简体   繁体   English

活动记录中的哈希条件

[英]Hash conditions in Active Record

As I know I can rewrite this active record query 据我所知,我可以重写此活动记录查询

Some.where("a = :a and b = :b", { :a => params[:a], :b => params[:b] })

this way: 这条路:

Some.where(:a => params[:a], :b => params[:b])

Now I need to rewrite this query: 现在,我需要重写此查询:

Some.where("a = :a and b > :b", { :a => params[:a], :b => params[:b] })

How can I get it ? 我怎么才能得到它 ?

I can use range conditions: 我可以使用范围条件:

Some.where(:a => params[:a], :b => params[:b]+1..100000)

But I can not be sure in the constant 100000. 但是我不能确定恒定的100000。

I can't see why you "need" to rewrite it. 我看不到您为什么“需要”重写它。 It works as-is, and there is no way to do it properly with hash-based conditions. 它按原样工作,并且无法在基于哈希的条件下正确执行。

That said, if you aren't a fan of SQL making a mess in your Ruby, you could take a look at the excellent Squeel gem which lets you rewrite things like 也就是说,如果您不喜欢将SQL弄乱Ruby的SQL爱好者,则可以看看出色的Squeel gem ,它可以让您重写诸如

where("name LIKE :name AND salary < :salary", {:name => "A%", :salary => 50000})

as

where{(name =~ "A%") & (salary < 50000)}

which I think is pretty cool. 我认为这很酷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM