The find method and referencing another table

All products have an order_id, but some orders are open and some are
closed. I need a way to find all products that belong to open orders
(the field status = 1 in the orders table).

Here’s the general idea of what I’m trying to do:

@products = Product.find(:all, :conditions=>[“order.status = ?”, “1”])

order.status obviously isn’t working because it’s a reference to a
different table. I’m having a hard time figuring out how to get this to
do what I need it to do.

Thanks!!

Patrick wrote:

All products have an order_id, but some orders are open and some are
closed. I need a way to find all products that belong to open orders
(the field status = 1 in the orders table).

Here’s the general idea of what I’m trying to do:

@products = Product.find(:all, :conditions=>[“order.status = ?”, “1”])

order.status obviously isn’t working because it’s a reference to a
different table. I’m having a hard time figuring out how to get this to
do what I need it to do.

Thanks!!

Add a joins clause to your find, eg :joins => 'INNER JOIN orders on
etc…"

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs