ruby on rails - How to compare if a record exist with json data type field? -
i want check if record exist on database, have 1 json data type field , need compare too. when try check using exists?
got following error:
select 1 one "arrangements" "arrangements"."deleted_at" null , "arrangements"."account_id" = 1 , "arrangements"."receiver_id" = 19 , "config"."hardware" = '--- category: mobile serial: ''00000013'' vehicle: ' , "arrangements"."recorded" = 't' limit 1 pg::undefinedtable: error: missing from-clause entry table "config" line 1: ...id" = 1 , "arrangements"."receiver_id" = 19 , "config"."... ^
code using check if exists:
@arrangement = arrangement.new({account_id: receiver.account.id, receiver_id: receiver.id, config: params[:config], recorded: true}) if arrangement.exists?(account_id: @arrangement.account_id, receiver_id: @arrangement.receiver_id, config: @arrangement.config, recorded: @arrangement.recorded) puts 'true' end
i tried:
if arrangement.exists?(@arrangement) puts 'true' end
but return false
table:
create_table :arrangements |t| t.references :account, index: true t.references :receiver, index: true t.json :config, null: false t.boolean :recorded, default: false t.datetime :deleted_at, index: true t.integer :created_by t.timestamps end
you cannot compare jsons. try compare jsons values
where("arrangements.config->>'category' = ?", params[:config][:category])
look in postgresql docs other json functions , operators
Comments
Post a Comment