Hi there.
I have two models (and two tables, of course), let’s call them Room and
Reserved, with similar fields but with the last one with some extra
info, and need to get data from both using a “union all”. Let’s think on
a hotel where I need to “glue” data to take a snapshot from the
occupation like:
Room (number,date,ocupied_by)
1,‘2006/06/01’,John
2,‘2006/06/01’,Paul
Reserved (number,reserved_on,reserved_by,expire_on)
3,‘2006/06/01’,George,‘2006/06/02 14:00’
4,‘2006/06/01’,Ringo ,‘2006/06/02 14:00’
select number, date, ocupied_by
from room
where date=‘2006/06/01’
union all
select number, reserved_on as date, reserved_by as ocupied_by
from reserved
where reserved_on=‘2006/06/01’
order by 1
How can I do that with ActiveRecord/Rails? Ok, I’m not so sure it’s a
good example/tables to illustrate the question, but the idea is using an
“union all” on the data from two tables as on regular SQL queries.
Thanks!