Select and group in rails 1.2

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

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?

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

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”

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”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs