【问题标题】:Metawhere abstract conditions from query来自查询的 Metawhere 抽象条件
【发布时间】:2012-06-04 01:41:09
【问题描述】:

我正在使用 Metawhere rails gem 来查询我的数据库。我有一个有效的查询:

t=Model.where({:attr1 => 450} | {:attr2 => 450}, :lang => 2)

现在我尝试像这样从查询中抽象出条件:

conditions=Hash[{:attr1 => 450} | {:attr2 => 450}, :lang => 2]
t=Model.where(conditions)

但第一行给了我:语法错误,意外的 tASSOC,期待 ']

我尝试了很多方法,例如在查询中将条件设为字符串和 eval(conditions)。但这给了我 SyntaxError: (eval):1:in `irb_binding': compile error (eval):1: 语法错误,意外的 ',',期待 $end

我被困住了。如果您能帮助我,将不胜感激。

罗格

【问题讨论】:

    标签: ruby-on-rails ruby activerecord meta-where


    【解决方案1】:

    替换

    conditions=Hash[{:attr1 => 450} | {:attr2 => 450}, :lang => 2}]
    

    conditions=Hash[{:attr1 => 450} | {:attr2 => 450}, :lang => 2]
    

    【讨论】:

    • Alex,Argh,对不起,我在上面的代码中打错了(我调整了它)。在真正的代码中,括号不存在,所以感谢您的提示,但不是解决我的问题
    • 那么,即使没有那个括号,你仍然有错误unexpected tASSOC, expecting ']
    • 顺便试试这个条件conditions = [{:attr1 => 450} | {:attr2 => 450}, :lang => 2]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多