'Query postgrsql JSONB array of objects field rails

I have variant model and I have option_values as JSONB field

class AddOptionValuesToVariants < ActiveRecord::Migration[6.0]
  def change
    add_column :variants, :option_values, :jsonb, default: {}
    add_index  :variants, :option_values, using: :gin
  end
end

I am trying to query on JSONB field but it fails and throws me an error,

ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: ERROR:  current transaction is aborted, commands ignored until end of transaction block

from /Users/hemant/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `exec'
Caused by PG::InFailedSqlTransaction: ERROR:  current transaction is aborted, commands ignored until end of transaction block

The query which I tried is,

str = "SELECT id, jsonb_array_elements(option_values) FROM variants;"
ActiveRecord::Base.connection.execute(str)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source