开发者

Find parents without or with children in polymorphic associations in rails

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
e开发者_运维问答nd

How I can find all cars only with child pictures?


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜