Ruby on Rails Ordering and Relationship Parents Kids

Asked

Viewed 35 times

1

I have a parent table and a child table, I would like to know how to make a query in the child table but sort by a parent table column. Like I want to know all the kids and order them by father’s age.

1 answer

1


Whereas there is a relationship has_many/belongs_to between the model Pai and the model Filho, you can do the following:

Filho.includes('pai').order('pais.idade DESC')

All right, all children will return by ordering by father’s age.

  • Thank you, it worked perfectly :D

  • @Andrégava uowww, good! : D needing we are there!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.