Query for multiple tables with find question :)

Hello all,

I am trying to figure out how to do this with find instead of doing it
via find_by_sql

For example I am trying to list all my completed orders, however I need
to check the progresses table to make sure the “currentlevel” in the
progress table equals
“endlevel” in the orders table.

The sql below works fine, but I am just curious how to do it another
way.

def completed

@orders = Order.find_by_sql ("SELECT * FROM orders, progresses WHERE
orders.id = progresses.user_id AND orders.endlevel =
progresses.currentlevel AND orders.process =1 ")

end

Your relationship of order.id to progresses.user_id confuses me. If
you
have progresses.order_id then you would be able to do this with find.
Is
there a reason why you’re associating the order with user_id?

Legacy DB - I have the it all linked up correct with the foreign key in
the model.

Brian H. wrote:

Your relationship of order.id to progresses.user_id confuses me. If
you
have progresses.order_id then you would be able to do this with find.
Is
there a reason why you’re associating the order with user_id?