views:

40

answers:

2

I have two models: cars and pictures in my RoR project

class Car < ActiveRecord::Base
  has_many :pictures, :as => :imageable, :dependent => :destroy
end

and

class Picture < ActiveRecord::Base
  belongs_to :imageable, :polymorphic => true, :dependent => :destroy
end

How I can find all cars only with child pictures?

A: 

This could get messy/slow, but one option is to iterate over all the cars and check to see if the number of children line up.

good_cars = []
Cars.all.each do |car|
  if (car.pictures.count > 0 && car.pictures.count == Picture.find_by_car_id(car.id).count)
    good_cars << car
  end
end

Or if you want a performance boost

good_cars = []
Cars.all.each do |car|
 if (car.pictures.count > 0)
    pic_count_for_car = Picture.count_by_sql(
      "SELECT COUNT(*) from pictures WHERE car_id = #{car.id}"
    )
    if (car.pictures.count == pic_count_for_car) 
      good_cars << car
    end
  end
end
Jamie Wong
I thought about this solution just in emergency case. But thanks anyway, this works too.
Colorblind
+1  A: 

I haven't tested it, but I think that something like that would make it in a single query.

class Car < ActiveRecord::Base
    has_many :pictures, :as => :imageable, :dependent => :destroy
    named_scope :with_child_picture, lambda { { 
        :joins => :pictures, 
        :group => "pictures.imageable_id", 
        :conditions => ["pictures.imageable_id != ?", nil] 
    } }
end

And you can use it as

Car.with_child_picture

I couldn't test it myself... but I hope at least it gives you an idea.

Francisco
Thanks for great solution! It worked for me. I just fixed a little expression :conditions => ["pictures.imageable_id IS NOT NULL"]
Colorblind