postgresql - Rails Postgres querying array of hash column -
i have table field of type 'json', using store array of hashes.
this schema looks like:
create_table "recommendations", force: true |t| t.text "message" t.json "vendors", default: {} t.datetime "created_at" t.datetime "updated_at" end this sample data:
>> recommendation.last.vendors [{"name" => "alphabeta"}, {"name" => "gamma"}] how recommendations includes or has vendor named "gamma"?
i tried this
>> recommendation.where("vendors ->> 'name' = 'gamma'").count but doesn't work.
update: found way answers here query element of array in json column
here is:
s = "select * recommendations t, json_array_elements(t.vendors) elem elem->>'name' = 'gamma';" activerecord::base.connection.execute(s).to_a
Comments
Post a Comment