简体   繁体   中英

Querying any member of a Postgres array type in Rails 4

Everywhere I looked there were 2 examples

either

Book.where("'history' = ANY (subjects)")

to query a book with a specific subject in the subject array

or

 Book.where("subjects @> ?", '{'history', 'drama'}')

to query books that the subjects array has both history and drama

How do I query for books that has either history or drama or both?

At the moment I am solving this using

query = subject_list.map do |subject|
                "'#{subject}' = ANY(subjects)"
            end.join(" OR ")

Book.where(query)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM