I have three models A, B, and C, where A has_many B
; B belongs to C
, and B has a created_on timestamp
. C has_many A through B.
I want to return all A where A has a B that belongs to C and where B is the most recently created B for that A.
I can do this with a method loop. Can this be done solely with named_scopes?, or is some other elegant/efficient manner?
As per request for real world (TM) examples A, B and C can be imagined as, for instance, Pets (A), PetsName (B), and Names (C). PetsNames are given to Pets at a specific time, and any given Name can be given to many Pets. Any Pet can have many Names (a :through relationship). For a given Name I want all of the Pets in which that Name is the most recently created PetName for that Pet. The call may look something like @name.pets.most_recently_named_as