passing dynamic parameter in a bloc

J

Josselin

in a RoR plugin, I am passing a string parameter (column name) i.e
'karma' as the sort parameter:

User.select { |u| u.id > 20 }.sort_by { |u| u.id }
"SELECT * FROM users WHERE (users.`karma` > 20) ORDER BY users.karma"

Is there any tric to pass a dynamic parameter as a variable ?
User.select { |u| u.id > 20 }.sort_by { |u| ...... }

I tried
@criteria = 'id'
User.select { |u| u.id > 20 }.sort_by { |u| u.send @criteria }

but it generates a wrong SQL
"SELECT * FROM users WHERE users.`id` > 20 ORDER BY users.send"


thanks for your help

joss
 
L

Logan Capaldo

in a RoR plugin, I am passing a string parameter (column name) i.e
'karma' as the sort parameter:

User.select { |u| u.id > 20 }.sort_by { |u| u.id }
"SELECT * FROM users WHERE (users.`karma` > 20) ORDER BY users.karma"

Is there any tric to pass a dynamic parameter as a variable ?
User.select { |u| u.id > 20 }.sort_by { |u| ...... }

I tried
@criteria = 'id'
User.select { |u| u.id > 20 }.sort_by { |u| u.send @criteria }

but it generates a wrong SQL
"SELECT * FROM users WHERE users.`id` > 20 ORDER BY users.send"


thanks for your help
This is really a rails specific question but here's a guess on how you
might be able to do it, if this doesn't work you should really try the
rails list. Actually you should probably try the rails list even if
this does work, there's almost certainly a better way to do this than
i what I am suggesting.

@criteria = 'id'
User.select { |u| u.id > 20 }.sort_by { |u| u.method_missing @criteria }
 
J

Josselin

This is really a rails specific question but here's a guess on how you
might be able to do it, if this doesn't work you should really try the
rails list. Actually you should probably try the rails list even if
this does work, there's almost certainly a better way to do this than
i what I am suggesting.

@criteria = 'id'
User.select { |u| u.id > 20 }.sort_by { |u| u.method_missing @criteria }

thanks a lot for your advice... that's true it's a Rail specific
question, ... the plugin's developer mentionned that it's not possible
at the present time to do what I want to do... (future release) but he
gave me a trick to go on without waiting for the new coming release....
this plugin is trying to hide all SQL calls behind a full Ruby
interface , already very useful.....
for interested people see :

http://errtheblog.com/post/10722

joss
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top