first_or_initialize不适用于json类型列

时间:2016-05-10 09:04:05

标签: ruby-on-rails json postgresql ruby-on-rails-4

我想在first_or_initialize类型列的表格上使用json

A.where("name=? and email=? and address->>'city'=? and address->>'state'=?", "John", "john@gmail.com", "New York", "New York").first_or_initialize

但是,这似乎不起作用。知道我们是否可以在这样的桌子上使用它?

2 个答案:

答案 0 :(得分:0)

请试试这个

A.where("'name'=? and 'email'=? and 'address.city'=? and 'address.state'=?", "John", "john@gmail.com", "New York", "New York").first_or_initialize

我认为这应该对你有帮助..

答案 1 :(得分:0)

根据Rails guide,您的请求似乎是正确的 正如他们所说:

  Query based on JSON document
  The -> operator returns the original JSON type (which might be an object), whereas ->> returns text

查询的返回是什么(没有.first_or_initialize)? 一个错误,一个空数组?

这些帖子可以帮助您构建所需的查询:Post1Post2PostgresqlHelp

编辑:对不起,我误解了这个问题

--------------------------------

如果请求有问题,您可以尝试:(从此处Rails guide

A.includes(:address).where(name: "John", email: "john@gmail.com", address: {city: "New York", state: "New York"})

如果请求不是问题,您能否提供有关错误的更多信息?