简体   繁体   中英

How to query array columns in Rails 4?

I can't find any good articles about how to query array columns in Rails. I came across the need to query an Array column in Rails.

I found from an article teaching how to do basic query here .

Let's follow the example in the article where Book covers many subjects and subjects is stored as an array column:

add_column :books, :subjects, :text, array: true, default: []

Query books that contains a certain subject - eg History

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

Query books that contains all listed subjects - eg Finance AND Business AND Accounting

Book.where("subjects @> ?", "{Finance,Business,Accounting}")

I wonder how I can do the following?

Query books that contains any of the listed subjects - eg Fiction OR Biography

Query books that doesn't contain a certain subject - eg NOT Physics

Query books that doesn't contain ANY of the subjects - eg NOT (Physics OR Chemistry OR Biology)

And is there any Rails way of doing the above queries?

For,

Query books that contains any of the listed subjects - eg Fiction OR Biography

Book.where("subjects &&  ?", "{Fiction,Biography}")

Query books that doesn't contain a certain subject - eg NOT Physics

Book.where("subjects <>  ?", "{Physics}")

Query books that don't contain ANY of the subjects - eg 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

  1. 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.

  2. Your queries:

Query books that contains any of the listed subjects - eg 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 - eg NOT Physics

Book.where.not("subjects @> ?", "{Physics}")

Query books that doesn't contain ANY of the subjects - eg 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)"

I might be missing something, but this is why you don't normally store this type of data in the database as an array (serialized or not), it makes complex queries much more difficult. I'd suggest creating additional model(s) which would allow you to use rails magic to do the kind of queries you're talking about.

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