Manually instantiate ActiveRecord models and their relationships?
If I have T-SQL (or a stored process) that returns records from multiple tables (perhaps using DBI), is there a way to manually create ActiveRecord instances and their associations? Obviously I'm following the database performance here. I would like to be able to create my own hierarchy of objects (models and their relationships), but when I did, I would expect each model to behave normally. That is, I hope this will be achieved without any kind of hack that could cause my framework to behave strangely.
EDIT:
It's a bit contrived, but it illustrates how a single query can return n data depth (where "n" has only practical limitations) and return everything in one call to the database:
SELECT * FROM customers
WHERE id = 1;
SELECT * FROM orders
WHERE customer_id = 1;
SELECT * FROM lineitems
WHERE order_id IN (
SELECT id FROM orders
WHERE customer_id = 1
);
And then, with all the records, I would just map the associations. The problem with doing this with ActiveRecord and: include is that it will hit the database multiple times, not just once - which is more taxing as "n" increases.
source to share
If I understand what you are getting at, you are trying to execute multiple sql queries at once and Rails returns all created instances as usual.
What do you really want:
Customer.find(:all, :include => {:orders => :lineitems})
Which will fetch all the records you are interested in in a single request and create AR objects correctly.
source to share