在 ActiveRecord 中帮助查找并包含条件
我有一个Coach
模型:
has_many :qualifications
我想找到所有attribute_id
为nil
并且他们有一些资格
的教练>。类似的东西。
def requirement
legal_coaches = []
coaches = find_all_by_attribute_id(nil)
coaches.each do |coach|
legal_coaches << coach if coach.qualifications.any?
end
legal_coaches
end
有没有一种方法可以将所有此类记录放在一行中?
I have a Coach
Model which:
has_many :qualifications
I want to find all coaches whose some attribute_id
is nil
and they have some qualifications
. Something which is like.
def requirement
legal_coaches = []
coaches = find_all_by_attribute_id(nil)
coaches.each do |coach|
legal_coaches << coach if coach.qualifications.any?
end
legal_coaches
end
Is there a way to get all such records in one line ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你不能通过纯粹的 ruby 语法来做到这一点。我只能想到以下(丑陋的)方式
I think you can't do that via purely ruby syntax. I can only think of the following (ugly) way