views:

32

answers:

2

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 ?

A: 

I think you can't do that via purely ruby syntax. I can only think of the following (ugly) way

Coach.find(:all, :conditions => "attribute_id IS NULL AND EXISTS(SELECT * FROM qualifications WHERE coach_id = coaches.id)")
neutrino
+1  A: 

find_all_by_attribute_id(nil).select(&:qualification)

Bragboy
But I said attribute_id how come we have manager_id here ?
Shikher
@shikher : corrected :)
Bragboy