Ruby on rails 在没有子查询的情况下查找不存在关联数据的所有数据

Ruby on rails 在没有子查询的情况下查找不存在关联数据的所有数据,ruby-on-rails,ruby,ruby-on-rails-4,postgresql-9.1,Ruby On Rails,Ruby,Ruby On Rails 4,Postgresql 9.1,我有两个表用户和联系人 使用者 接触 | id | user_id | contact |active | category_id | |:-----------|-----------:|:-------------:|:---------:|:------------:| | 1 | 1 | 132423 | false | 2 | 2 | 1

我有两个表
用户
联系人

使用者

接触

| id         | user_id    | contact       |active     | category_id  |
|:-----------|-----------:|:-------------:|:---------:|:------------:|
| 1          |      1      |    132423    |    false  |     2   
| 2          |      1      |    131423    |    true   |     3
| 3          |      1      |    142423    |    true   |     1
| 4          |      2      |    132413    |    true   |     1
| 5          |      2      |    132425    |    false  |     2
| 6          |      2      |    131420    |    true   |     3
| 7          |      3      |    131323    |    true   |     3
| 8          |      3      |    332423    |    true   |     1
| 9          |      3      |    232423    |    false  |     2
| 10         |      4      |    132523    |    false  |     1
我希望类别1的用户没有活动的
(false)
,或者没有该类别联系人的用户以及以下联系人

| id         | name        | last_name    |
|:-----------|------------:|:------------:|
| 4          |      Smith  |    Warne   
| 5          |      Brad   |    Pitt
我尝试了一些类似跟踪的方法,但没有成功

User.includes(:contacts).where(contacts: {user_id: nil, category_id: 1 })
我可以使用两个不同的查询来完成它,但不知道如何在一个查询中编写它

u_ids = Conatct.where(category_id: 1, active: true).pluck(:user_id)
users = User.where.not(id: u_ids)
eager_load在“contacts”表上执行左外部联接。对于所有未与contacts表关联的用户,此查询的contacts.id将以nil结尾

User.eager\u load(:contacts)。其中(contacts:{id:nil,category\u id:1})
eager\u load在“contacts”表上执行左外部联接。对于所有未与contacts表关联的用户,此查询的contacts.id将以nil结尾
u_ids = Conatct.where(category_id: 1, active: true).pluck(:user_id)
users = User.where.not(id: u_ids)
User.eager_load(:contacts).where(contacts: {id: nil, category_id: 1 })