views:

74

answers:

2

Following is the association between 2 models:

class FotoGossip < ActiveRecord::Base
   has_many :uploads
end

class Upload < ActiveRecord::Base
   belongs_to :foto_gossip
end

@latest_uploads = Upload.all(:include => :foto_gossip, :order => "created_at DESC", :limit => 5)

It displays the latest 5 photos from Upload model.
But, I want to display 5 images from Uploads, order_by created_date DESC but only 1 image per FotoGossip.
Its something like grouping the recent FotoGossip with its one photo from Uploads model.

A: 

I think you can use ActiveRecord::Base#calculate like in

@latest = Update.maximun(:creted_at,:distinct=>:foto_gossip_id)
Fer
well, thanks for the reply Fer!. But it didn't work as I expected and I did my own way as on the answer I posted.
Millisami
A: 

This following AR query solved it.

@latest_uploads = Upload.all(:include => :foto_gossip, :order => "created_at DESC", :limit => 5, :group => :foto_gossip_id)

The magic lies in the :group option.

Millisami