How to query array columns in Rails 4?
Usually, associations are a preferable way of approaching the problem:
Book has_many :subjects # or has_one/has_and_belongs_to_many
Subject belongs_to :book # or has_and_belongs_to_many
And then just create a table
subjects
, save all your subjects there and you're set up.Your queries:
Query books that contains any of the listed subjects - e.g. Fiction OR Biography
Book.find_by_sql "SELECT * FROM books WHERE 'Fiction' = ANY (subjects) OR 'Biography' = ANY (subjects)"
Query books that doesn't contain a certain subject - e.g. NOT Physics
Book.where.not("subjects @> ?", "{Physics}")
Query books that doesn't contain ANY of the subjects - e.g. NOT (Physics OR Chemistry OR Biology)
Book.find_by_sql "SELECT * FROM books WHERE books NOT IN (SELECT * FROM books WHERE 'Physics' = ANY (subjects) OR 'Chemistry' = ANY (subjects) OR 'Biology' = ANY (subjects)"
For,
Query books that contains any of the listed subjects - e.g. Fiction OR Biography
Book.where("subjects && ?", "{Fiction,Biography}")
Query books that doesn't contain a certain subject - e.g. NOT Physics
Book.where("subjects <> ?", "{Physics}")
Query books that don't contain ANY of the subjects - e.g. NOT (Physics OR Chemistry OR Biology)
Book.where.not("subjects && ?", "{Physics,Chemistry,Biology}")
You can see the array functions of Postgres for reference.
https://www.postgresql.org/docs/8.2/functions-array.html