Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Union Queries

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() ?

like image 832
Trt Trt Avatar asked Jan 22 '26 22:01

Trt Trt


1 Answers

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

like image 133
Rebitzele Avatar answered Jan 24 '26 13:01

Rebitzele



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!