[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

passing dynamic parameter in a bloc

Josselin

10/1/2007 2:33:00 PM

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

2 Answers

Logan Capaldo

10/1/2007 8:58:00 PM

0

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

Josselin

10/2/2007 10:25:00 AM

0

On 2007-10-01 22:58:25 +0200, "Logan Capaldo" <logancapaldo@gmail.com> said:

> 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

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/...

joss