# Concatenating parts of sql queris

**URL:** <https://www.ruby-forum.com/t/concatenating-parts-of-sql-queris/147515>\
**Category:** Rails\
**Created:** [September 2, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/concatenating-parts-of-sql-queris/147515 "2008-09-02T17:23:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![blueshadow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@blueshadow](https://www.ruby-forum.com/u/blueshadow)\
**Post date:** [September 2, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/concatenating-parts-of-sql-queris/147515/1 "2008-09-02T17:23:15Z")

</div>

I’m trying to develop a full-text search filtered by location and  
categories!  
My ugly way is to write different sql query’s for different solution,  
but i realize that is just stupid and ugly and didn’t do the job!  
Nifty\_scaffold don’t work for me, i need the custom job!  
Are concatenating the parts of sql is hard in ruby?  
Can someone pass the link of tutorial or solution to this problem of  
mine?  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![blueshadow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@blueshadow](https://www.ruby-forum.com/u/blueshadow)\
**Post date:** [September 2, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/concatenating-parts-of-sql-queris/147515/2 "2008-09-02T17:27:42Z")

</div>

Are you talking about doing Unions in SQL?  
Or do you have multiple distinct results from different tables that  
you want to aggregate?

---

<div class="post-metadata">

**Author:** ![blueshadow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@blueshadow](https://www.ruby-forum.com/u/blueshadow)\
**Post date:** [September 2, 2008, 5:40pm UTC](https://www.ruby-forum.com/t/concatenating-parts-of-sql-queris/147515/3 "2008-09-02T17:40:20Z")

</div>

Jorg L. wrote:

> Are you talking about doing Unions in SQL?  
> Or do you have multiple distinct results from different tables that  
> you want to aggregate?

Jorg  
I’m talking about setting up a parts of sql like  
var1 = SELECT  
jobs.id,  
jobs.location,  
jobs.title,  
jobs.company,  
jobs.description,  
jobs.url  
FROM  
jobs  
var2 = WHERE  
jobs.id \> 0  
AND jobs.id = jobs\_categories.job\_id  
…  
var3 = ta na na na GROUP BY categories.id  
ORDER BY categories.name ASC

```auto
and if defined?(some_value) then
query = var1+vart3
else
 !defined?(some_value) then
 query = var1+vart2+var4
else
 !defined?(some_value) then
 query = var1+vart3+var4
end
something like that!
Or if u have a bather solution I'm listening! :)
Thanks!
```

---

<div class="post-metadata">

**Author:** ![blueshadow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@blueshadow](https://www.ruby-forum.com/u/blueshadow)\
**Post date:** [September 2, 2008, 5:50pm UTC](https://www.ruby-forum.com/t/concatenating-parts-of-sql-queris/147515/4 "2008-09-02T17:50:34Z")

</div>

Oh I see. That’s what I do. I actually have a model (not an active  
record base) that just generates SQL and it does exactly what you  
have. Input depends on the specific function, the pieces are added  
with ruby and the final sql in returned. Then I just access the  
methods from whatever controller needs them. It keeps all the sql  
generation in one place.

---

<div class="post-metadata">

**Author:** ![blueshadow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@blueshadow](https://www.ruby-forum.com/u/blueshadow)\
**Post date:** [September 2, 2008, 5:56pm UTC](https://www.ruby-forum.com/t/concatenating-parts-of-sql-queris/147515/5 "2008-09-02T17:56:19Z")

</div>

Jorg L. wrote:

> Oh I see. That’s what I do. I actually have a model (not an active  
> record base) that just generates SQL and it does exactly what you  
> have. Input depends on the specific function, the pieces are added  
> with ruby and the final sql in returned. Then I just access the  
> methods from whatever controller needs them. It keeps all the sql  
> generation in one place.  
> Sounds cool and clean.  
> Can you tell me hay u do it please.  
> thanks!
