# Using unions in ActiveRecord

**URL:** <https://www.ruby-forum.com/t/using-unions-in-activerecord/112233>\
**Category:** Rails\
**Created:** [August 16, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/using-unions-in-activerecord/112233 "2007-08-16T17:43:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [August 16, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/using-unions-in-activerecord/112233/1 "2007-08-16T17:43:00Z")

</div>

I am at a complete loss of how to do this in an ActiveRecord:

Example

select concat(c.last\_name, ', ', c.first\_name,) as name  
from customer c inner join basketball b on  
(c.id=b.cust\_id)  
where b.start\_date \> ‘8/1/2007’  
union  
select concat(c.last\_name, ', ', c.first\_name,) as name  
from customer c inner join football f on  
(c.id=f.cust\_id)  
where b.start\_date \> ‘8/1/2007’  
union  
select concat(c.last\_name, ', ', c.first\_name,) as name  
from customer c inner join soccer s on  
(c.id=s.cust\_id)  
where b.start\_date \> ‘8/1/2007’  
order by name

Any help would be greatly appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [August 16, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/using-unions-in-activerecord/112233/2 "2007-08-16T17:57:34Z")

</div>

find\_by\_sql 😉

On 8/16/07, George P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## select concat(c.last\_name, ', ', c.first\_name,) as name Any help would be greatly appreciated. Thanks.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [August 16, 2007, 11:35pm UTC](https://www.ruby-forum.com/t/using-unions-in-activerecord/112233/3 "2007-08-16T23:35:38Z")

</div>

So there is no way to use AR to do this? Or do I need to hack my own  
solution…

Pratik Naik wrote:

> find\_by\_sql 😉
> 
> On 8/16/07, George P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ## select concat(c.last\_name, ', ', c.first\_name,) as name Any help would be greatly appreciated. Thanks.
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >
> 
> –  
> Cheers!
> 
> - Pratik  
> [http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [August 17, 2007, 5:17am UTC](https://www.ruby-forum.com/t/using-unions-in-activerecord/112233/4 "2007-08-17T05:17:02Z")

</div>

To clarify, I know how to do this using find\_by\_sql. I figure I need to  
use ActiveRecord because I need to pass parameters to the query, such as  
the date field. I can’t find a way to pass a parameter using  
find\_by\_sql.

> Pratik Naik wrote:
> 
> > find\_by\_sql 😉
> > 
> > On 8/16/07, George P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > ## select concat(c.last\_name, ', ', c.first\_name,) as name Any help would be greatly appreciated. Thanks.
> > > 
> > > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > > 
> > > >
> > 
> > –  
> > Cheers!
> > 
> > - Pratik  
> > [http://m.onkey.org](http://m.onkey.org)
