sql - Rails query the last of each type -


i using sti , have table widget bunch of other subclasses inherit using sti. want query gets last created of each object uniq type.

i have predefined array of types want lets say:

types = ["type1", "type2", "type3"] 

so query like: (this doesnt work)

widget.where(type: types).uniq.reverse 

my goal last of each object matches 1 of types..

not 100% sure, might work (untested):

ids = thing.where(type: types).group(:type).maximum(:id).values last_per_type = thing.find(ids) 

Comments

Popular posts from this blog

java - HTTP Status 500 - An exception occurred processing JSP page /second.jsp at line 15 -

SQL Server - MyCTE query based on 24 hour period (next day) -

php - Using str_replace to translate a MySQL Table in html -