I have the following query that I was trying get using the ActiveRecord Query Interface:
SELECT *, null, null, null FROM model A
WHERE A.column IN ( 2, 3 )
UNION SELECT * FROM model A2 INNER JOIN other_model B ON B.other_column = A2.id
WHERE B.column IN ( 2, 3 );
The null
in the select is because of the different number of columns in the two tables.
I want to get all the records from model
plus the records from model
joined with other_model
even if the record is already in the first query.
I tried to use only AR with Model.joins()
but It returned me only the second part of the query, plus I'd like to do only one query. I've tried using Arel too, but I'm not much familiar with It and not worked.
I ended up with Model.find_by_sql('raw_query')
but I'm sure that's a Rails way to achieve the same result. Could some one help me?