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

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -