我想在我的 Rails 应用程序 (v 3.2.6) 中使用 Squeel gem(基于 Arel)。我的 hstore 列称为属性。
这些工作得很好:
User.where{(firstname == 'Ryan') & (lastname == 'Bates')}
User.where{"properties @> ('male' => '1')"}
第二个示例是一个普通的 Postgres 查询,因为 Squeel 似乎不支持 hstore 函数。
这些不起作用:
User.where{"properties @> ('male' => '1')" & firstname == 'Ryan'}
User.where{("properties @> ('male' => '1')") & (firstname == 'Ryan')}
错误:
NoMethodError: undefined method `&' for "properties @> ('male' => '1')":String
我确实理解该错误,但我不知道如何封装我的 hstore 查询。有没有更好的方法使用 Squeel 或 Arel 构建 hstore 查询?
最佳答案
Squeel 通过使用反引号 (`) 支持 SQL 文字。
像下面这样的东西可能会起作用:
Person.where{(id == my{@person.id}) & (`preferences @> send_me_junk_email=>yes`)}
当使用反引号时,Squeel 会降低一层抽象并直接执行 SQL。
http://erniemiller.org/2012/05/30/sql-literals-in-squeel-or-overriding-backticks-in-ruby/
关于ruby-on-rails - 如何在 Rails 中将 Squeel 与 hstore 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12284273/