Skip to content
Advertisement

Ordering records by an association presence

Lets say I have an instructions table which is associated to a surveys table through survey_instructions join table.

What I need to achieve is to fetch all instruction records, but ordered by an association presence with a given survey.

So instructions associated with a given survey will go first, and then all other instructions which have no association with this survey.

Could this be achieved by chaining active record queries somehow? Would appreciate any thoughts on this

Advertisement

Answer

Yes you can achieve this by ActiveRecord query. Try this:

Happy coding 🙂

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement