Searching for Generic Objects by attribute value

I needed to find Generic Objects based on attribute values.

vmdb_production=# \d generic_objects;
                                              Table "public.generic_objects"
            Column            |            Type             |                          Modifiers
 id                           | bigint                      | not null default nextval('generic_objects_id_seq'::regclass)
 name                         | character varying           |
 uid                          | character varying           |
 generic_object_definition_id | bigint                      |
 created_at                   | timestamp without time zone | not null
 updated_at                   | timestamp without time zone | not null
 properties                   | jsonb                       | default '{}'::jsonb
    "generic_objects_pkey" PRIMARY KEY, btree (id)

So the name of the column is properties and its type is JSON. To find all objects with status == 'in-use' I ended up with the following code based on this howto:

$evm.vmdb(:generic_object).where("properties ->> 'status' = 'in-use'")

If the actual value is in a variable:

value = 'in-use'
$evm.vmdb(:generic_object).where("properties ->> 'status' = '" + value + "'")


Method find_objects defined on GenericObjectDefinition can be used for the search based on GenericObject attributes and attributes defined in properties column.
For example:

$evm.vmdb(:generic_object_definition, => 'in-use', :attr2 => value2)
1 Like