passing dynamic parameter in a bloc

Discussion in 'Ruby' started by Josselin, Oct 1, 2007.

  1. Josselin

    Josselin Guest

    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
     
    Josselin, Oct 1, 2007
    #1
    1. Advertising

  2. On 10/1/07, Josselin <> 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
    >
    >
    >
     
    Logan Capaldo, Oct 1, 2007
    #2
    1. Advertising

  3. Josselin

    Josselin Guest

    On 2007-10-01 22:58:25 +0200, "Logan Capaldo" <> said:

    > On 10/1/07, Josselin <> 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/post/10722

    joss
     
    Josselin, Oct 2, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?q?Nicolas_Herv=E9?=

    gcc + gprof : basic bloc counting

    =?iso-8859-1?q?Nicolas_Herv=E9?=, Jul 25, 2007, in forum: C Programming
    Replies:
    2
    Views:
    366
    Richard
    Jul 26, 2007
  2. Josselin

    exit in a bloc DRY way...

    Josselin, Feb 18, 2007, in forum: Ruby
    Replies:
    8
    Views:
    124
    SonOfLilit
    Feb 18, 2007
  3. Yaya __

    Proc&Bloc

    Yaya __, Apr 11, 2007, in forum: Ruby
    Replies:
    2
    Views:
    93
    Yaya Sow
    Apr 11, 2007
  4. Tj Holowaychuk

    Bloc param parens |memo, (a, b)|

    Tj Holowaychuk, Jan 16, 2009, in forum: Ruby
    Replies:
    2
    Views:
    88
    Tj Holowaychuk
    Jan 16, 2009
  5. Jake Barnes
    Replies:
    2
    Views:
    153
    Richard Cornford
    Feb 12, 2006
Loading...

Share This Page