Ruby w/ Postgres & Sinatra - Query won't order right with parameter??

Posted by alleywayjack on Stack Overflow See other posts from Stack Overflow or by alleywayjack
Published on 2010-04-30T15:52:45Z Indexed on 2010/04/30 15:57 UTC
Read the original article Hit count: 192

Filed under:
|
|
|

So I set a variable in my main ruby file that's handling all my post and get requests and then use ERB templates to actually show the pages. I pass the database handler itself into the erb templates, and then run a query in the template to get all (for this example) grants.

In my main ruby file:

grants_main_order = "id_num"
get '/grants' do
    erb :grants, :locals => {:db=>db, :order=>grants_main_order, :message=>params[:message]}
end

In the erb template:

db = locals[:db]
getGrants = db.exec("SELECT * FROM grants ORDER BY $1", [locals[:order]])

This produces some very random ordering, however if I replace the $1 with id_num, it works as it should.

Is this a typing issue? How can I fix this? Using string replacement with #{locals[:order]} also gives funky results.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about sinatra