kid*_*rax 28 ruby ruby-on-rails arel ruby-on-rails-3
你怎么能做条件where子句?我有一个运行查询的rake任务.假设我正在构建一个这样的查询:
residentials = Residential.where(:is_active => true)
Run Code Online (Sandbox Code Playgroud)
现在,如果我将某个参数传递给rake任务,我想添加到where子句.我在想这样的事情:
residentials.where(:something_else => true) if param_was_passed
Run Code Online (Sandbox Code Playgroud)
但这只是替换现有的where子句.如何将其添加到现有的where子句中?
Ber*_*als 34
可以将语句链接到哪里
residentials = Residential.where(:is_active => true)
residentials = residentials.where(:other_thing => true) if param_was_passed
Run Code Online (Sandbox Code Playgroud)
这应该工作.
确保这不是函数调用的最后一行; residentials在这种情况下,将变量重复为最后一行.(根据@ digger69评论)
Jit*_*its 20
您可以构建哈希,然后将其提供给.where方法.就像是:
h = { }
h[:is_active] = true
h[:field_x] = true if param_was_passed
residentials = Residential.where(h)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11872 次 |
| 最近记录: |