# Select and group in rails 1.2

**URL:** <https://www.ruby-forum.com/t/select-and-group-in-rails-1-2/88193>\
**Category:** Rails\
**Created:** [March 8, 2007, 9:03pm UTC](https://www.ruby-forum.com/t/select-and-group-in-rails-1-2/88193 "2007-03-08T21:03:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marco\_Kotrotsos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@Marco\_Kotrotsos](https://www.ruby-forum.com/u/Marco_Kotrotsos)\
**Post date:** [March 8, 2007, 9:03pm UTC](https://www.ruby-forum.com/t/select-and-group-in-rails-1-2/88193/1 "2007-03-08T21:03:58Z")

</div>

Hi there.

Is there a bug in 1.2 anyone knows of that generates faulty sql?

I have

@jobs = Recommendation.find(:all, :conditions =\> [“job\_id = ?”,  
“#{params[:id]}%”], :group =\> ‘job\_id’,:select =\> [:tracker, :job\_id])

but it generates

SELECT trackerjob\_id FROM recommendations WHERE (job\_id = ‘3%’) GROUP BY  
job\_id

see? it leaves out the comma in the selected columns.

Did anyone see this before? Is there a fix for it?

Thanks people!  
Marco

---

<div class="post-metadata">

**Author:** ![Marco\_Kotrotsos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@Marco\_Kotrotsos](https://www.ruby-forum.com/u/Marco_Kotrotsos)\
**Post date:** [March 8, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/select-and-group-in-rails-1-2/88193/2 "2007-03-08T21:15:59Z")

</div>

that’s because :select expects a string, not an array. the array is  
getting converted to a string and that is the result

irb(main):001:0\> [:tracker, :job\_id].to\_s  
=\> “trackerjob\_id”

so you want

:select =\> “tracker, job\_id”

also, your condition is a bit strange.

is job\_id an integer column? why are you using = and %? you want  
every record where job\_id begins with 3?

---

<div class="post-metadata">

**Author:** ![Marco\_Kotrotsos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@Marco\_Kotrotsos](https://www.ruby-forum.com/u/Marco_Kotrotsos)\
**Post date:** [March 9, 2007, 8:50am UTC](https://www.ruby-forum.com/t/select-and-group-in-rails-1-2/88193/3 "2007-03-09T08:50:38Z")

</div>

Chris H. wrote:

> that’s because :select expects a string, not an array. the array is  
> getting converted to a string and that is the result
> 
> irb(main):001:0\> [:tracker, :job\_id].to\_s  
> =\> “trackerjob\_id”
> 
> so you want
> 
> :select =\> “tracker, job\_id”
> 
> also, your condition is a bit strange.
> 
> is job\_id an integer column? why are you using = and %? you want  
> every record where job\_id begins with 3?

Ye I know, i fixed the sql- also it was just mostly dummy sql to show  
the point (the % was an typo)  
So I guess O’Reilly’s Quick reference in ‘Ruby on Rails up and running’  
on page 145 is wrong. it clearly stated

:select =\> [:name, :adress]

or should i have just read it “name, adress” ?  
Thank alot!  
Marco

---

<div class="post-metadata">

**Author:** ![Marco\_Kotrotsos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@Marco\_Kotrotsos](https://www.ruby-forum.com/u/Marco_Kotrotsos)\
**Post date:** [March 9, 2007, 12:50pm UTC](https://www.ruby-forum.com/t/select-and-group-in-rails-1-2/88193/4 "2007-03-09T12:50:46Z")

</div>

if you are passing in [:x, :y] and getting back “xy” then that means  
the array is being converted to a string. pass it a string.

from the api

:select: By default, this is \* as in SELECT \* FROM, but can be changed  
if you for example want to do a join, but not include the joined  
columns.

from the source

```
    def construct_finder_sql(options)
      scope = scope(:find)
      sql = "SELECT #{(scope && scope[:select]) ||

```

options[:select] || ‘\*’} "

so as you can see, options[:select] in the above

“#{[:a, :b]}” =\> “ab”

---

<div class="post-metadata">

**Author:** ![Marco\_Kotrotsos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@Marco\_Kotrotsos](https://www.ruby-forum.com/u/Marco_Kotrotsos)\
**Post date:** [March 9, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/select-and-group-in-rails-1-2/88193/5 "2007-03-09T16:00:49Z")

</div>

Thanks Chris, this as been most helpfull  
kind regards,  
Marco

Chris H. wrote:

> if you are passing in [:x, :y] and getting back “xy” then that means  
> the array is being converted to a string. pass it a string.
> 
> from the api
> 
> :select: By default, this is \* as in SELECT \* FROM, but can be changed  
> if you for example want to do a join, but not include the joined  
> columns.
> 
> from the source
> 
> ```
> def construct_finder_sql(options)
> scope = scope(:find)
> sql = "SELECT #{(scope && scope[:select]) ||
> 
> ```
> 
> options[:select] || ‘\*’} "
> 
> so as you can see, options[:select] in the above
> 
> “#{[:a, :b]}” =\> “ab”
