dennismonsewicz
dennismonsewicz

Reputation: 25542

Ruby ActiveRecord Maximum

I am working with a Rails app that I need to return the maximum value for a particular row but also need to return the rest of the row contents.

Code:

medias = Media.maximum(:id, :conditions => ['medias.image = true AND medias.subscriber_id  = 37'], :group => ['subscriber_id'])

This is only returning the id and subscriber_id, I need to return all of the rows not just those two.

Example: In my medias table a subscriber can upload multiple photos. I am trying to group by the subscriber id and get the last entry for all subscribers. So basically I need a find all by maximum.

Upvotes: 3

Views: 8269

Answers (2)

fl00r
fl00r

Reputation: 83680

medias = Media.maximum(:id, :conditions => ['medias.image = true AND medias.subscriber_id  = 37'], :group => ['subscriber_id'])
trythis = Media.find(medias)

Upvotes: 6

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

Media.order("subscriber_id DESC").limit(1).where("image = true").where("subscriber_id = ?", 37)

I think you're wanting to find the max subscriber_id, but if not, just replace that with what you're ordering on

Upvotes: 2

Related Questions