Ruby-on-rails 关于模型关联的 Rails where() 子句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6334537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails where() clause on model association?
提问by Derek J
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!
谢谢!
回答by Dogbert
Try this:
尝试这个:
User.joins(:account).where(:accounts => { :active => true })
回答by Simon Perepelitsa
You need to join the accounts table and merge appropriate Account scope:
您需要加入帐户表并合并适当的帐户范围:
User.joins(:account).merge(Account.where(:active => true))
回答by Jared Menard
Use a where clause in the Account model's association:
在 Account 模型的关联中使用 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:
您还可以在同一个表上指定 2 个关系:
class Account < ActiveRecord::Base
has_many :users
has_many :active_users, -> {where(active: true)}, :class_name => 'User'
2nd Update:
第二次更新:
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
回答by Maxime Lapointe
A gem that exists to do that: activerecord_where_assoc(I'm the author)
一个存在的宝石:activerecord_where_assoc(我是作者)
With it, you can do what you want this way:
有了它,你可以用这种方式做你想做的事:
@users_of_active_accounts = User.where_assoc_exists(:account, active: true)
And if you have a scope on Account for active, you could call it this way:
如果您在 Account 上有一个作用域,您可以这样称呼它:
@users_of_active_accounts = User.where_assoc_exists(:account, &:active)
So now, if you want, you can make a nice scope for this:
所以现在,如果你愿意,你可以为此创建一个很好的范围:
class User < ActiveRecord::Base
belongs_to :account
scope :active_account, -> { where_assoc_exists(:account, active: true) }
end
@users_of_active_accounts = User.active_account
Read more in the documentation. Here is an introductionand examples.
回答by Nickolay Efimov
Try this:
尝试这个:
Account.includes(:users).where(active: true)

