开发者

Ruby ActiveRecord Maximum

开发者 https://www.devze.com 2023-02-16 15:07 出处:网络
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 co开发者_高级运维ntents.

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 co开发者_高级运维ntents.

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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消