模型关联中的Rails where()子句?

时间:2020-12-19 08:18:26

Suppose I have an Account model which has_many Users. Accounts have a boolean "active" column. How can I get all Users that belong to "active" accounts?

假设我有一个拥有许多用户的帐户模型。帐户有一个布尔“活动”列。如何让所有属于“活跃”帐户的用户?

@users_of_active_accounts = User.?

Thanks!

谢谢!

3 个解决方案

#1


44  

You need to join the accounts table and merge appropriate Account scope:

您需要加入帐户表并合并适当的帐户范围:

User.joins(:account).merge(Account.where(:active => true))

#2


68  

Try this:

试试这个:

User.joins(:account).where(:accounts => { :active => true })

#3


7  

Use a where clause in the Account model's association:

在帐户模型的关联中使用where子句:

class Account < ActiveRecord::Base
  has_many :users, -> {where(active: true)}

The other queries will work, but if you only always care about active users, filtering at the association level will properly encapsulate the filter and save you headaches in the future.

其他查询可以工作,但是如果您只关心活动用户,那么在关联级别上的过滤将适当地封装过滤器,并在将来为您省去麻烦。

Update:

更新:

You can also specify 2 relations on the same table:

您也可以在同一个表上指定两个关系:

 class Account < ActiveRecord::Base
   has_many :users
   has_many :active_users, -> {where(active: true)}, :class_name => 'User'

2nd Update:

2日更新:

After re-reading the question, I now see that my answer didn't answer the question. Here's my answer to the question:

重读这个问题后,我发现我的答案并没有回答这个问题。我的回答是:

User.where(account: Account.where(active: true))

3rd Update: Here's an example User model that has an active_users attribute

第三个更新:这是一个具有active_users属性的示例用户模型

class User < ActiveRecord::Base
  belongs_to :account
  def self.active
    where(account: Account.where(active: true))
  end
end

Doing it this way allows you to put it inline with other user queries:

通过这种方式,您可以将它与其他用户查询放在一起:

User.active.where(created_at: (1.week.ago..0.day.ago)).count

#1


44  

You need to join the accounts table and merge appropriate Account scope:

您需要加入帐户表并合并适当的帐户范围:

User.joins(:account).merge(Account.where(:active => true))

#2


68  

Try this:

试试这个:

User.joins(:account).where(:accounts => { :active => true })

#3


7  

Use a where clause in the Account model's association:

在帐户模型的关联中使用where子句:

class Account < ActiveRecord::Base
  has_many :users, -> {where(active: true)}

The other queries will work, but if you only always care about active users, filtering at the association level will properly encapsulate the filter and save you headaches in the future.

其他查询可以工作,但是如果您只关心活动用户,那么在关联级别上的过滤将适当地封装过滤器,并在将来为您省去麻烦。

Update:

更新:

You can also specify 2 relations on the same table:

您也可以在同一个表上指定两个关系:

 class Account < ActiveRecord::Base
   has_many :users
   has_many :active_users, -> {where(active: true)}, :class_name => 'User'

2nd Update:

2日更新:

After re-reading the question, I now see that my answer didn't answer the question. Here's my answer to the question:

重读这个问题后,我发现我的答案并没有回答这个问题。我的回答是:

User.where(account: Account.where(active: true))

3rd Update: Here's an example User model that has an active_users attribute

第三个更新:这是一个具有active_users属性的示例用户模型

class User < ActiveRecord::Base
  belongs_to :account
  def self.active
    where(account: Account.where(active: true))
  end
end

Doing it this way allows you to put it inline with other user queries:

通过这种方式,您可以将它与其他用户查询放在一起:

User.active.where(created_at: (1.week.ago..0.day.ago)).count