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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -