Rails 4: sort by association attribute -
person has_many :dogs
dog belongs_to :person
dog has_many :bones
bone belongs_to :dog
i want find bone associated each dog given person owns , sort results bone_buried_date
something like...
bones = [] some_person.dogs.each |dog| bones << dog.bones.first end bones.sort_by{ |e| e[:bone_buried_date] }
seems clumsy. wondering if there better way.
try one
bone.where(dog: dog.where(person: some_person)).order(:bone_buried_date)
Comments
Post a Comment