[RUBY] How to query Array in jsonb with Rails + postgres

Note because it was difficult

Data shape

Something like this is stored in a jsonb column.

{
  hoge: String,
  items: String[]
}

What you want to do and the solution

So what I want to do is query ʻitemsthat contains one of the specific strings[" a "," b "," c "]`.

In conclusion, I could use the following code.

items_to_query = ["a", "b", "c"]
Model.where("jsonbColumnName -> 'items' @> ?", items_to_query.to_json)

reference

https://stackoverflow.com/questions/35737931/rails-postgres-query-with-jsonb-array https://www.postgresql.org/docs/9.4/functions-json.html

Recommended Posts

How to query Array in jsonb with Rails + postgres
[Rails] How to write in Japanese
How to introduce jQuery in Rails 6
How to get along with Rails
How to install Swiper in Rails
How to build Rails, Postgres, ElasticSearch development environment with Docker
How to implement search functionality in Rails
How to change app name in rails
How to use custom helpers in rails
[Rails] How to use rails console with docker
How to insert a video in Rails
How to use MySQL in Rails tutorial
How to get boolean value with jQuery in rails simple form
[rails] How to configure routing in resources
How to rename a model with foreign key constraints in Rails
How to implement ranking functionality in Rails
How to use credentials.yml.enc introduced in Rails 5.2
How to build Rails 6 environment with Docker
How to create a query using variables in GraphQL [Using Ruby on Rails]
[Rails] How to log in with a name by adding a devise name column
[Rails] How to get the user information currently logged in with devise
How to display the text entered in text_area in Rails with line breaks
[Rails] How to apply the CSS used in the main app with Administrate
[Rails] How to use select boxes in Ransack
How to translate Rails into Japanese in general
How to prevent direct URL typing in Rails
How to separate .scss by controller in Rails
How to conditionally add html.erb class in Rails
One way to redirect_to with parameters in rails
How to implement a like feature in Rails
How to easily create a pull-down in Rails
[Rails] How to easily implement numbers with pull-down
[Ruby on Rails] How to install Bootstrap in Rails
[Rails] How to build an environment with Docker
How to make a follow function in Rails
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
How to write Rails
How to uninstall Rails
[Rails] How to register multiple records in the intermediate table with many-to-many association
How to embed JavaScript variables in HTML with Thymeleaf
How to implement UICollectionView in Swift with code only
How to sort in ascending / descending order with SQLite
How to make batch processing with Rails + Heroku configuration
[Rails] How to search by multiple values ​​with LIKE
How to implement guest login in 5 minutes in rails portfolio
How to implement a like feature in Ajax in Rails
[Ruby on Rails] How to write enum in Japanese
How to switch Tomcat context.xml with WTP in Eclipse
How to push an app developed with Rails to Github
How to delete a new_record object built with Rails
How to use Z3 library in Scala with Eclipse
How to make an almost static page with rails
How to manually generate a JWT with Rails Knock
Organized how to interact with the JDK in stages
[Ruby On Rails] How to reset DB in Heroku
How to output standard from an array with forEach
[Rails] How to deal with URL changes after render
How to delete untagged images in bulk with Docker
How to write a date comparison search in Rails
How to use JDD library in Scala with Eclipse