# Passing dynamic parameter in a bloc

**URL:** <https://www.ruby-forum.com/t/passing-dynamic-parameter-in-a-bloc/116008>\
**Category:** Ruby\
**Created:** [October 1, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/passing-dynamic-parameter-in-a-bloc/116008 "2007-10-01T16:35:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 1, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/passing-dynamic-parameter-in-a-bloc/116008/1 "2007-10-01T16:35:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 1, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/passing-dynamic-parameter-in-a-bloc/116008/2 "2007-10-01T23:00:18Z")

</div>

On 10/1/07, Josselin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 }

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 2, 2007, 12:27pm UTC](https://www.ruby-forum.com/t/passing-dynamic-parameter-in-a-bloc/116008/3 "2007-10-02T12:27:23Z")

</div>

On 2007-10-01 22:58:25 +0200, “Logan C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](http://errtheblog.com/post/10722)

joss
