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
Post a Comment