on rails 5 with PostgreSQL 12.2, I have this model
create_table "training_opportunities", force: :cascade do |t|
t.bigint "contact_id"
t.string "situation"
t.boolean "cpf"
t.string "software_interest"
t.string "training_type_interest"
t.string "month"
t.text "comment"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "status"
t.boolean "pole_emploi", default: false
t.jsonb "history", default: [], array: true
t.boolean "to_be_call", default: false
t.boolean "is_flag", default: false
t.boolean "is_star", default: false
t.index ["contact_id"], name: "index_training_opportunities_on_contact_id"
end
I try to query TrainingOpportunity by the content of the history. For example, in rails c
TrainingOpportunity.first.history
give
[{"date"=>"2020-07-03 09:05:00 +0000", "step"=>"ID POLE EMPLOI : 1242050E - architecture intérieur - débutante qlq notions de 3D - environ 2500€CPF - devis envoyé le 03/07"}, {"date"=>"20/07/03", "step"=>"reprise automatique de l'existant"}]
I try to find it whith this query:
TrainingOpportunity.where('history @> {"step":"ID POLE EMPLOI : 1242050E - architecture intérieur - débutante qlq notions de 3D - environ 2500€CPF - devis envoyé le 03/07"}')
but I've got this error
ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR: syntax error at or near "{")
LINE 1: ...* FROM "training_opportunities" WHERE (history @> {"step":"I...
^
: SELECT "training_opportunities".* FROM "training_opportunities" WHERE (history @> {"step":"ID POLE EMPLOI : 1242050E - architecture intérieur - débutante qlq notions de 3D - environ 2500€CPF - devis envoyé le 03/07"}) LIMIT $1
I tried :
TrainingOpportunity.where('history @> ?', '[{"step":"ID POLE EMPLOI : 1242050E - architecture intérieur - débutante qlq notions de 3D - environ 2500€CPF - devis envoyé le 03/07"}]')
but i got this error:
ActiveRecord::StatementInvalid (PG::InvalidTextRepresentation: ERROR: malformed array literal: "[{"step":"ID POLE EMPLOI : 1242050E - architecture intérieur - débutante qlq notions de 3D - environ 2500€CPF - devis envoyé le 03/07"}]")
LINE 1: ...* FROM "training_opportunities" WHERE (history @> '[{"step":...
^
DETAIL: "[" must introduce explicitly-specified array dimensions.
: SELECT "training_opportunities".* FROM "training_opportunities" WHERE (history @> '[{"step":"ID POLE EMPLOI : 1242050E - architecture intérieur - débutante qlq notions de 3D - environ 2500€CPF - devis envoyé le 03/07"}]') LIMIT $1
after rmlockerd proposition , I tried the following solution
but no ones works.
TrainingOpportunity.where('history @> ?', {"date"=>"20/07/03"}.to_json)
TrainingOpportunity Load (1.0ms) SELECT "training_opportunities".* FROM "training_opportunities" WHERE (history @> '{"date":"20/07/03"}') LIMIT $1 [["LIMIT", 11]]
Traceback (most recent call last):
ActiveRecord::StatementInvalid (PG::InvalidTextRepresentation: ERROR: malformed array literal: "{"date":"20/07/03"}")
LINE 1: ...* FROM "training_opportunities" WHERE (history @> '{"date":"...
^
DETAIL: Unexpected array element.
: SELECT "training_opportunities".* FROM "training_opportunities" WHERE (history @> '{"date":"20/07/03"}') LIMIT $1
TrainingOpportunity.where('history @> ?', {'date'=>'20/07/03'}.to_json)
TrainingOpportunity Load (1.0ms) SELECT "training_opportunities".* FROM "training_opportunities" WHERE (history @> '{"date":"20/07/03"}') LIMIT $1 [["LIMIT", 11]]
Traceback (most recent call last):
ActiveRecord::StatementInvalid (PG::InvalidTextRepresentation: ERROR: malformed array literal: "{"date":"20/07/03"}")
LINE 1: ...* FROM "training_opportunities" WHERE (history @> '{"date":"...
^
DETAIL: Unexpected array element.
: SELECT "training_opportunities".* FROM "training_opportunities" WHERE (history @> '{"date":"20/07/03"}') LIMIT $1
TrainingOpportunity.where('history @> ?', {date:'20/07/03'}.to_json)
TrainingOpportunity Load (1.0ms) SELECT "training_opportunities".* FROM "training_opportunities" WHERE (history @> '{"date":"20/07/03"}') LIMIT $1 [["LIMIT", 11]]
Traceback (most recent call last):
ActiveRecord::StatementInvalid (PG::InvalidTextRepresentation: ERROR: malformed array literal: "{"date":"20/07/03"}")
LINE 1: ...* FROM "training_opportunities" WHERE (history @> '{"date":"...
^
DETAIL: Unexpected array element.
: SELECT "training_opportunities".* FROM "training_opportunities" WHERE (history @> '{"date":"20/07/03"}') LIMIT $1
TrainingOpportunity.where('history @> ?', [{date:'20/07/03'}].to_json)
TrainingOpportunity Load (1.0ms) SELECT "training_opportunities".* FROM "training_opportunities" WHERE (history @> '[{"date":"20/07/03"}]') LIMIT $1 [["LIMIT", 11]]
Traceback (most recent call last):
ActiveRecord::StatementInvalid (PG::InvalidTextRepresentation: ERROR: malformed array literal: "[{"date":"20/07/03"}]")
LINE 1: ...* FROM "training_opportunities" WHERE (history @> '[{"date":...
^
DETAIL: "[" must introduce explicitly-specified array dimensions.
: SELECT "training_opportunities".* FROM "training_opportunities" WHERE (history @> '[{"date":"20/07/03"}]') LIMIT $1
I try to specify the first element of the array
TrainingOpportunity.where('history --> 0 = ?', '{"date"=>"20/07/03"}')
TrainingOpportunity Load (0.9ms) SELECT "training_opportunities".* FROM "training_opportunities" WHERE (history --> 0 = '{"date"=>"20/07/03"}') LIMIT $1 [["LIMIT", 11]]
Traceback (most recent call last):
ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR: syntax error at end of input)
LINE 1: ...ies" WHERE (history --> 0 = '{"date"=>"20/07/03"}') LIMIT $1
^
: SELECT "training_opportunities".* FROM "training_opportunities" WHERE (history --> 0 = '{"date"=>"20/07/03"}') LIMIT $1