How do I find records based on attributes of a many-to-many-related model?

邮差的信 提交于 2019-12-11 03:26:18

问题


Models...

InternalUser
  has_many :internal_user_roles
  has_many :roles, :through => :internal_user_roles

InternalUserRole
  belongs_to :internal_user
  belongs_to :role

Role
  has_many :internal_user_roles
  has_many :internal_users, :through => :internal_user_roles

Using the new ActiveRecord query API, how would I find all the InternalUsers with the "ADMIN" role?

In other words, how do I generate this query...

SELECT
  *
FROM
  internal_users i, internal_user_roles ir, roles r
WHERE
  i.id = ir.internal_user_id
AND
  r.id = ir.internal_user_id
AND
  r.name = 'ADMIN'

回答1:


Ideally you should start with an object that was the admin role:

role = Role.find_by_name('ADMIN')

Then you could simply query the internal users for that role:

role.internal_users

If you want to go further, you should note that all associations in ActiveRecord can be further queried upon:

role.internal_users.where(:first_name => 'Bill').limit(5)

Getting back to the original question, alternatively you could query the InternalUser model:

InternalUser.includes(:roles).where(['roles.id = ?', role])

or perhaps a bit faster, but more complex code-wise:

InternalUser.includes(:internal_user_roles).where(['internal_user_roles.role_id = ?', role])

To translate your SQL query directly, you could also do this:

InternalUser.includes(:roles).where("roles.name = 'ADMIN'")

You can also see the SQL that ActiveRecord would generate (for any of these queries) by putting a 'to_sql' call at the end, like so:

InternalUser.includes(:roles).where("roles.name = 'ADMIN'").to_sql


来源:https://stackoverflow.com/questions/10149970/how-do-i-find-records-based-on-attributes-of-a-many-to-many-related-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!