我有一个Coach
模型:
has_many :qualifications
我想找到一些attribute_id
为nil
并且有一些qualifications
的教练。有点像。
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
有没有办法将所有这些记录放在一行?
答案 0 :(得分:1)
find_all_by_attribute_id(nil).select(&:qualification)
答案 1 :(得分:0)
我认为你不能通过纯粹的ruby语法来做到这一点。我只能想到以下(丑陋的)方式
Coach.find(:all, :conditions => "attribute_id IS NULL AND EXISTS(SELECT * FROM qualifications WHERE coach_id = coaches.id)")