I'd like to find the value from the array type column to array.

Asked 2 years ago, Updated 2 years ago, 120 views

There is a table as below.

Books Table
-----------------------
COLUMN NAME  |  TYPE
id           |  integer
name         |  character varying
subjects     |  character varying []

subjects array types

when is stored as the {education, business, history} like this.

I want to search for {history,business} in subjects.(For OR conditions, either history or business is fine.) How can I search for array type?

Book.where (subjects: ["hisotyr, business"]) didn't work.

postgresql ruby-on-rails querying

2022-09-22 22:12

1 Answers

Book.where(Book.arel_table[:subjects].overlap(['hisotyr', 'business']))
or
Book.where.overlap(subjects: ['hisotyr', 'business'])

Book.where(Book.arel_table[:subjects].contains(['hisotyr', 'business']))
or
Book.where.contains(subjects: ['hisotyr', 'business'])

Please use the one that suits your needs. Above is &&& in sql, below is @>.


2022-09-22 22:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.