postgres#从某事物中选择*;
id |名字| random_id |
位置|
created_at | updated_at |类型|
细节---- + ---------- + ------------------------------- ------- + ------------------------------------------ -------------------------------------------------- ----------------------------- + -------------------- -------- + ---------------------------- + ----------- + -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ----------------------------------------
15 |一个| 6abddaba-6b6e-4433-ae3b-698a16971704 | somehttp://one.tar.xz | 2017-05-02 06:55:36.146826 | 2017-05-02 06:55:48.154355 | OpenStack的 | {“os”:[{“release”:“Fedora”}],“openstack_version”:[{“buildhost”: “openstack.com”,“package_name”:“nova”,“package_version”:1.2, “openstack_version”:8}],“architecture”:[{“kernel”:“x86_64”}], “volume_drivers”:[{“drivers”:“ABCDriver”}],“network_drivers”:[], “network_plugins”:[],“shared_file_system_drivers”:[]}
16 |两个| 9b13fb4c-2e20-4336-b486-473b50948ee1 | somehttp://two.tar.xz | 2017-05-02 06:55:52.219204 | 2017-05-02 06:56:03.212226 | OpenStack的 | {“os”:[{“release”:“Fedora”}],“openstack_version”:[{“buildhost”: “openstack.com”,“package_name”:“nova”,“package_version”:1.2, “openstack_version”:8}],“architecture”:[{“kernel”:“x86_64”}], “volume_drivers”:[{“drivers”:“XYZDriver”}],“network_drivers”:[], “network_plugins”:[],“shared_file_system_drivers”:[]}
17 |三个| 119eaf5f-166d-4ddd-a4db-d1f56bbf8779 | somehttp://three.tar.xz | 2017-05-02 06:56:58.009536 | 2017-05-02 06:57:06.538191 | openstack | {“os”:[{“release”: “Fedora”}],“openstack_version”:[{“buildhost”:“openstack.com”, “package_name”:“nova”,“package_version”:1.3,“openstack_version”: 9}],“architecture”:[{“kernel”:“x86_64”}],“volume_drivers”:[], “network_drivers”:[{“drivers”:“linuxbridge”}],“network_plugins”: [{“plugins”:“ml2”}],“shared_file_system_drivers”:[]}
19 |四个| 139eaf5f-166d-4ddd-a4db-d1f56bbf8779 | somehttp://four.tar.xz | 2017-05-02 06:56:58.009536 | 2017-05-02 06:57:06.538191 | OpenStack的 | {“os”:[{“release”:“Fedora”}],“openstack_version”:[{“buildhost”: “openstack.com”,“package_name”:“nova”,“package_version”:1.3, “openstack_version”:9}],“architecture”:[{“kernel”:“x86_64”}], “volume_drivers”:[],“network_drivers”:[{“drivers”:“bridge”}], “network_plugins”:[{“plugins”:“pqrst”}], “shared_file_system_drivers”:[]}
我想在名为“something”的表中找到条目,其中“volume_drivers”可以是“ABCDriver”或“XYZDriver”,network_drivers可以是“linuxbridge”。
因此,根据上述条件,我的结果应包含id为15,16,17的条目。
我尝试使用postgres shell执行相同的操作,但是,我无法将相同的postgres查询映射到ruby。
select * from something where details->'volume_drivers' IN ('[{"drivers": "ABCDriver"}]','[{"drivers": "XYZDriver"}]') OR details->'network_drivers' IN ('[{"drivers": "linuxbridge"}]');
我尝试如下以获取ABCDriver的条目并得不到任何结果:(
Something.where('details->volume_drivers @> ?', [{"drivers": "ABCDriver"}]);
另外,如果有人有更好的建议来检索它,请告诉我。
答案 0 :(得分:0)
试试这段代码:
Something.where("details->>'volume_drivers' IN (?)",
[[{"drivers": "ABCDriver"}], [{"drivers": "XYZDriver"}]].map(&:to_json))
# result sql
SELECT "somethings".* FROM "somethings"
WHERE (details->>'volume_drivers' IN
('[{"drivers":"ABCDriver"}]','[{"drivers":"XYZDriver"}]'))