Hellboy
Hellboy

Reputation: 1239

PG::UndefinedTable: ERROR: missing FROM-clause entry for table

I have a table A having columns name(string), email(sring) and details(json). I want to use first_or_create like this:

A.where(:name => a.name, :email => a.email, :details => a.details).first_or_create

But this gives the following error:

PG::UndefinedTable: ERROR: missing FROM-clause entry for table "details"

Can't we query like the above if we have a json type column?

Upvotes: 2

Views: 892

Answers (1)

Hellboy
Hellboy

Reputation: 1239

I figured it out. Turns out, for JSON, the query will be like this:

A.where("name=? and email=? and details->>'key1'=? and details->>'key2'=?", a.name, a.email,a.details["key1"],a.details["key2"])

http://edgeguides.rubyonrails.org/active_record_postgresql.html#json

Upvotes: 1

Related Questions