Passing dynamic parameter in a bloc

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

On 10/1/07, Josselin [email protected] wrote:

@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 }

On 2007-10-01 22:58:25 +0200, “Logan C.” [email protected]
said:

I tried
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 }

joss

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

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