hash in hash, iterating the members

Discussion in 'Ruby' started by Depili, Jan 12, 2006.

  1. Depili

    Depili Guest

    I'm quite new to ruby and I'm learning with ruby on rails, but I have
    hit a brick wall, because I want to parse several fields from a web
    form which give the same data in a loop and maintaining the order from
    the web form.

    So I have a form with fields like:

    Data[1][Name] Data[1]Notes .....
    Data[2][Name] Data[2]Notes .....

    and the params hash looks like

    {"Data" = {"1" = {"Name" = "foo", "Notes"="bar"}, "2" = {.....

    I can access the fields easily when the keynames are strings, but with
    numbered keys params[:Data][1] doesn't work. Also params[Data].each do
    |data| leads to errors,,,

    I would like to get a way of accessing the hashes inside data for
    creating new database entries in rails.
    Depili, Jan 12, 2006
    #1
    1. Advertising

  2. I suppose you've used PHP before? In Ruby, strings and numbers are NOT =20
    equivalent in any context I can think of. at least by default. More =20
    notably:

    irb(main):001:0> 1.hash
    =3D> 3
    irb(main):002:0> "1".hash
    =3D> 50
    irb(main):003:0> :Data.hash
    =3D> 929038
    irb(main):004:0> "Data".hash
    =3D> 141011718

    As you can see, the number 1 has a different hash code than the string =20
    "1", same forthe symbol :Data and the string "Data".

    Make a simple rails view that will dump params as text and check if you =20
    are indeed indexing the data by the actual keys in the hash.

    David Vallner

    On Thu, 12 Jan 2006 11:28:06 +0100, Depili <> wrote:

    > I'm quite new to ruby and I'm learning with ruby on rails, but I have
    > hit a brick wall, because I want to parse several fields from a web
    > form which give the same data in a loop and maintaining the order from
    > the web form.
    >
    > So I have a form with fields like:
    >
    > Data[1][Name] Data[1]Notes .....
    > Data[2][Name] Data[2]Notes .....
    >
    > and the params hash looks like
    >
    > {"Data" =3D {"1" =3D {"Name" =3D "foo", "Notes"=3D"bar"}, "2" =3D {....=
    David Vallner, Jan 12, 2006
    #2
    1. Advertising

  3. Depili

    Depili Guest

    I can access the data with the :Data keys, but question remains, when I
    have keys :1..:10 for example, how do I access them in a loop?
    Depili, Jan 12, 2006
    #3
  4. On Thu, 12 Jan 2006 16:58:04 +0100, Depili <> wrote:

    > I can access the data with the :Data keys, but question remains, when I
    > have keys :1..:10 for example, how do I access them in a loop?
    >
    >



    Hmm, this still seems strange, AFAIK, Rails sanitizes form input. Are you=
    =20
    sure you aren't circumventing what the Rails framework provides? A more =20
    concise approach would be to let ActiveRecord do its magic if possible.

    Anyhoo, you can still iterate over the data if for some strange reason th=
    e =20
    request parameter parser uses symbols like that as keys, e.g.:

    for i in (1..10)
    do_stuff_with(params[:Data][i.to_s.to_sym])
    end

    Anyways, more context wouldn't hurt, but if the above does the trick, fin=
    e =20
    as well.

    David Vallner
    David Vallner, Jan 12, 2006
    #4
  5. Depili

    Depili Guest

    That did the trick, thanks.

    I'm pretty sure that I'm not overidin rails in any way, I just have a
    form temlate and I get the data from it by http post method and rails
    parses the data into the params hash table.

    But this will get me going forward :)
    Depili, Jan 13, 2006
    #5
  6. On Fri, 13 Jan 2006 01:13:09 +0100, Depili <> wrote:

    > That did the trick, thanks.
    >
    > I'm pretty sure that I'm not overidin rails in any way, I just have a
    > form temlate and I get the data from it by http post method and rails
    > parses the data into the params hash table.
    >
    > But this will get me going forward :)
    >
    >



    What I was thinking is if you shouldn't use ActiveRecords to store the =20
    data and let it sort out the data types - I smell lack of data model =20
    definition / normalization a bit. Of course, if it's not persitent data, =
    =20
    it's not a good approach.

    David Vallner
    David Vallner, Jan 13, 2006
    #6
    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. JFCM
    Replies:
    4
    Views:
    5,711
  2. CoolPint
    Replies:
    8
    Views:
    944
    Jeff Schwab
    Dec 14, 2003
  3. carl
    Replies:
    5
    Views:
    2,318
    James Kanze
    Nov 25, 2009
  4. rp
    Replies:
    1
    Views:
    477
    red floyd
    Nov 10, 2011
  5. Steven Hirsch

    Iterating over a hash of hash of hashes

    Steven Hirsch, Aug 19, 2008, in forum: Ruby
    Replies:
    0
    Views:
    142
    Steven Hirsch
    Aug 19, 2008
Loading...

Share This Page