简体   繁体   English

在 Rails 中,如何在一个查找器中查询两个 has_many 关联?

[英]In Rails, how do I query two has_many associations in a single finder?

I'm using Rails 4.2.我正在使用 Rails 4.2。 I have the following user model with a couple of has_many associations我有以下用户 model 有几个 has_many 关联

class User < ActiveRecord::Base
    …
  has_many :roles, through: :roles_users
  has_many :addresses, dependent: :destroy, as: :addressable, inverse_of: :addressable

I would like to find all users of a specific role without any addresses.我想找到没有任何地址的特定角色的所有用户。 I thought the below would do it我以为下面会做到这一点

> users = User.includes(:roles, :addresses).where(:roles => {:name => 'User'}, :addresses => {:user_id => nil})

But when I check the results, I'm still getting results that have addresses …但是当我检查结果时,我仍然得到有地址的结果......

2.7.1 :012 > users.last.addresses.count
…
 => 2

What's the proper way to write a finder that queries these two has_many associations?编写查询这两个 has_many 关联的查找器的正确方法是什么?

Checking for children records with a nil parent id is like the way of doing this in Rails 4. But if that doesn't work, you could use the NOT IN clause combination:检查具有 nil 父 ID 的子记录就像在 Rails 4 中执行此操作的方式一样。但是如果这不起作用,您可以使用 NOT IN 子句组合:

User
  .where
  .not(
    id: User
          .joins(:addresses, :roles)
          .where(roles: { name: 'User' })
          .select(:id)
  )

It's basically filtering out by the user id all those user rows that have an address associated, have a role, and the role name is exactly "User".它基本上是通过用户 ID 过滤掉所有具有关联地址、具有角色且角色名称恰好是“用户”的用户行。 You end up with a SQL query like this:您最终会得到一个 SQL 查询,如下所示:

SELECT "users".*
FROM "users"
WHERE "users"."id" NOT IN (
  SELECT "users"."id"
  FROM "users"
  INNER JOIN "roles_users" ON "roles_users"."user_id" = "users"."id"
  INNER JOIN "roles" ON "roles"."id" = "roles_users"."role_id"
  WHERE "roles"."name" = 'User'
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Rails 3中显示has_many关联的属性? - How do I show attributes for has_many associations in Rails 3? 你如何为 has_many 关系编写 Rails finder 查询? - How do you write a Rails finder query for a has_many relation? 如何在Rails中结合两个has_many关联用于一个实例和一个集合? - How to combine two has_many associations for an instance and a collection in Rails? 在Rails 5.1 中,如何为has_many 和belongs_to 关联编写finder 方法? - In Rails 5.1, how do I write a finder method for a has_many and then belongs_to association? Rails / SimpleForm:如何在表单中嵌套has_many关联的字段? - Rails/SimpleForm: How do I nest fields for has_many associations in a form? Rails关联:如何通过多个自引用条件来限制/限定has_many:? - Rails associations: How do I limit/scope a has_many :through with multiple self-referencing conditions? 如何使用has_many / belongs_to使Rails 4关联自动保存到两个表中? - How do I make Rails 4 associations automatically save to both tables using has_many / belongs_to? 如何通过has_many查询关联: - how to query associations with a has_many :through 我如何编写一个 Rails finder 方法,其中 has_many 项目都没有非 nil 字段? - How do I write a Rails finder method where none of the has_many items has a non-nil field? 如何编写 Rails finder 方法来搜索我的模型,其中 has_many 没有特定属性? - How do I write a Rails finder method to search for my model where the has_many has no particular attribute?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM