views:

180

answers:

3

Is it possible to limit an AR :include to say only pull in one record...

Item.find(:all,
    :include => [ :external_ratings, :photos => LIMIT 1 ])

I have a list of items and each item has between 5 and 15 photos. I want to load a photo id into memory, but i don't need all of them, I just want to preview the first one.

Is there a way to do this?

A: 

I don't believe it is possible to do it directly, but as finding one record is only a single query, why not do it outside the include?

e.g

first_photo = Photo.find(records.first.photo_id)

After your main find

DanSingerman
i'm sure this would work find, but if I have 50 entries it'll have to do it 50 different times. It works fine right now with the include, it's just pulling into memory more info than I need. ;-(
holden
Can you clarify what you want in your question? You either want 1record or 50, but it is not clear what you are trying to do.
DanSingerman
Sorry, I changed it a little. But basically I'm retrieving a list of items, and each item has multiple images and ratings. I want all the ratings included, but I only need one image for each item, basically a preview.
holden
Is there any ordering to the photos for each item?
DanSingerman
no, just the date they get added, which doesn't matter.
holden
i suppose using :include on photos isn't a huge footprint, i just thought there might be an easy way to limit it.
holden
No easy way I think. It must be possible using SQL aggregate functions (using the AR :select and :group options) but can not quite get my head round it now.
DanSingerman
A: 

I'm not going to investigate the detailed query, but I'm thinking find_by_sql is in order for this particular case.

Barry Hess
A: 

Any body have any luck with this problem?

Daniel