On 10/1/07, Josselin <josselin / wanadoo.fr> wrote:
> 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 }

> joss
>
>
>