Forum: Ruby on Rails Query for multiple tables with find question :)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
24164437d5b28c974280ffc9e9dd6045?d=identicon&s=25 John B. (funbags)
on 2006-06-13 21:27
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
Brian Hogan (Guest)
on 2006-06-13 21:53
(Received via mailing list)
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?
24164437d5b28c974280ffc9e9dd6045?d=identicon&s=25 John B. (funbags)
on 2006-06-13 22:21
Legacy DB - I have the it all linked up correct with the foreign key in
the model.


Brian Hogan 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?
This topic is locked and can not be replied to.