views:

65

answers:

1

I have three models:

class Address < ActiveRecord::Base
 has_many :jobs
end

class Category < ActiveRecord::Base
  has_many :jobs, :dependent => :destroy
end

class Job < ActiveRecord::Base
 belongs_to :category
 belongs_to :address
end

I'm trying to get all categories that the jobs has address_id != NULL I'm almost there doing:

categories = Category.find(:all,:joins=>:jobs,:conditions=>'jobs.address_id is not null')

the problem is that i have a lot of repeated categories. I can solve it doing a:

categories.uniq!

but is not exactly the best solution.. Any idea?

+2  A: 

Do you mean you have different categories with the same category name? If so you can try this:

categories = Category.find(:all,:joins=>:jobs,
           :conditions=>'jobs.address_id is not null', :group => "category.name")

Note: If there are several categories with the same name you will get one of them. You have no control over the specific row returned to you.

KandadaBoggu
i must review the results, but at least you were in the right direction.
VP