Three models: Product, Image, Document
Relations: A product has many images and documents.
Images and products has a flag telling whether or not they are
published.
I want to show a product and include only documents and images that are
published.
I was hoping I could do this in my controller:
@product = Product.find(params[:id], :include => [:images, :documents],
:conditions => ‘(images.published=1 AND documents.published=1’)
That worked great as long as the product had at least one published
image and at least one published document. If not, I would get a
recordnotfound exception.
What I’m currently doing is this:
@product = Product.find(params[:id], :include => [:images, :documents])
@product.documents.delete_if{|d| d.published == false}
@product.images.delete_if{|i| i.published == false}
This works but I’m curious if the original query can be modified to
handle cases where a product doesnt have at least one published image
and document. Can it?
-S