hi,
I have a table for example Product, and another table called Picture
(Product has many pictures, Picture belongs to Product). Now let’s say
that i want to retrieve all the Product and its related pictures, i do
this with:
@products = Product.find(:all, :include => :pictures)
But in my case, i need to add a condition to the above code, like so:
@products = Product.find(:all, :include => :pictures, :conditions =>
“pictures.main = 1”)
So, i will retrive all the products and its related MAIN pictures,
but…if one product does not have any picture, @products will not
have that product, so i am unable to show the product, because it does
not have any picture. Now Rails generate this sql for me
SELECT products
.id
AS t0_r0, products
.name
,
product_pictures
.id
AS t1_r0, product_pictures
.parent_id
AS
t1_r1 FROM products
LEFT OUTER JOIN product_pictures
ON
product_pictures.product_id = products.id WHERE
(product_pictures.principal = 1)
But why i really need is something like:
SELECT products
.id
AS t0_r0, products
.name
,
product_pictures
.id
AS t1_r0, product_pictures
.parent_id
AS
t1_r1 FROM products
LEFT OUTER JOIN product_pictures
ON
product_pictures.product_id = products.id AND
product_pictures.principal = 1
Any idea of how can i do this?
Regards