I understand that it is written in SQL, but I was worried about how to write it in Rails, so I will write it
What I wanted to know
SELECT * FROM Tables WHERE (name, task) not in ((yamada, "running"),(yamada, "swimming"));
I had a lot of trouble, but I couldn't put it together well, so I solved it with a subquery! If you pass it as an array, you can pass multiple items, and if you make a subquery like this, you can create the conditions you want!
Queries are issued as expected
Table.where.not(id: (Table.select(:id).where(name: yamada).where(task: ["running", "swimming"])))
In the first policy, I thought that I could narrow down the conditions by repeating the where clause multiple times, but if I write the following, I will play all the tasks of "yamada". The SQL that is actually issued is also described below. I wrote while drawing a Venn diagram in my head.
A query different from the expected value is issued
Table.where.not(name: yamada).where.not(task: ["running", "swimming"])
Queries different from expected value
SELECT `tables`.* FROM `tables` WHERE `tables`.`deleted_at` IS NULL AND (`tables`.`name` != "yamada") AND (`tables`.`task` NOT IN ("running", "swimming"))
Rails Guide (https://railsguides.jp/active_record_querying.html)
Recommended Posts