How can I execute a union (outer join) query in rails/active record?
...
FROM `parents`
LEFT JOIN `child` on parents.child_id = child.id
UNION
...
FROM `parents`
RIGHT JOIN `child` on parents.child_id = child.id
ORDER BY rate desc, grade desc, date desc;
..using Model.joins() ?
There isn't currently a way to do it using the regular ActiveRecord syntax, although such an idea has been proposed: https://github.com/rails/rails/issues/939
Until such a way exists, you can accomplish your goal using the find_by_sql method, described here: http://guides.rubyonrails.org/active_record_querying.html#finding-by-sql
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With