Query for multiple tables with find question :)


#1

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


#2

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?


#3

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?