eager loading

19 views
Skip to first unread message

stark

unread,
Sep 8, 2012, 12:52:10 PM9/8/12
to scooter-...@googlegroups.com
Yo John,

I got another question....

re eager loading. In the docs....


-- start snip --

To retrieve a pet owner along with all the pets he/she has and each pet's type in one query (eager loading):

ActiveRecord owner6 = Owner.where("owners.id=6").includes("pets=>visits, pets=>type").getRecord();

The SQL equivalent of the above is:

SELECT OWNERS.ID AS OWNERS_ID, OWNERS.FIRST_NAME AS OWNERS_FIRST_NAME,
    OWNERS.LAST_NAME AS OWNERS_LAST_NAME, OWNERS.ADDRESS AS OWNERS_ADDRESS,
    OWNERS.CITY AS OWNERS_CITY, OWNERS.TELEPHONE AS OWNERS_TELEPHONE,
    PETS.ID AS PETS_ID, PETS.NAME AS PETS_NAME, PETS.BIRTH_DATE AS PETS_BIRTH_DATE,
    PETS.TYPE_ID AS PETS_TYPE_ID, PETS.OWNER_ID AS PETS_OWNER_ID,
    VISITS.ID AS VISITS_ID, VISITS.PET_ID AS VISITS_PET_ID,
    VISITS.VISIT_DATE AS VISITS_VISIT_DATE, VISITS.DESCRIPTION AS VISITS_DESCRIPTION,
    OWNERS_PETS.ID AS OWNERS_PETS_ID, OWNERS_PETS.NAME AS OWNERS_PETS_NAME,
    OWNERS_PETS.BIRTH_DATE AS OWNERS_PETS_BIRTH_DATE,
    OWNERS_PETS.TYPE_ID AS OWNERS_PETS_TYPE_ID,
    OWNERS_PETS.OWNER_ID AS OWNERS_PETS_OWNER_ID,
    TYPES.ID AS TYPES_ID, TYPES.NAME AS TYPES_NAME
FROM OWNERS LEFT OUTER JOIN PETS ON OWNERS.ID=PETS.OWNER_ID
            LEFT OUTER JOIN VISITS ON PETS.ID=VISITS.PET_ID
            LEFT OUTER JOIN PETS OWNERS_PETS ON OWNERS.ID=OWNERS_PETS.OWNER_ID
            LEFT OUTER JOIN TYPES ON OWNERS_PETS.TYPE_ID=TYPES.ID
WHERE OWNERS.ID = 6


-- end snip --



Let's say i got 100 columns on the 'visit' table, how can i just do it by just specifying the columns that i want?, say i only want these columns ...

VISITS.ID AS VISITS_ID, VISITS.PET_ID AS VISITS_PET_ID,
    VISITS.VISIT_DATE AS VISITS_VISIT_DATE,

It seems that when using "include:" (eager) it still calls all columns even though i have an association(.. i am testing hasOne()..)  on the Owner model for the Visit model, which has the options 'columns:'

But with lazy loading, all works as expected.

Hope you can give light to my question. Let me know if my question is clear.


Many thanks again!



John Chen

unread,
Sep 9, 2012, 9:55:45 PM9/9/12
to scooter-...@googlegroups.com
Ok, I will think about this request.
 
But a quick hack is to create a view based on the table you want and put only selected columns in the view. Then you can build an ActiveRecord model on the view.

stark

unread,
Sep 9, 2012, 10:36:01 PM9/9/12
to scooter-...@googlegroups.com
I think that this would be the most important update with the next patch/release of scooter.

Also if you can also consider doing an update (query builder or activerecord) for specifying columns whenever you call just the very same model.
For example if I were to load a Visit model (with 100 columns in the actual db table), It would be nice if you can also specify columns that you want. Right now, it works with the TableGateway class. 

Thanks again John!
Reply all
Reply to author
Forward
0 new messages