SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question delete from hash

    Hi guys,
    I need to serialize the attributes of the models I have in order to build a string with all the submitted values.


    All the model have the attribute "checksum" but I don't want adding it to the string. I wrote the code below:

    Code:
          serialized = ""
           self.attributes.delete['checksum']
           self.attributes.each do |key,value|
              if value.is_a?(String)   
                serialized += value
              else
                serialized += value.to_s
              end
            end
    Basically I try to delete from the hash "attributes" the element with key="checksum" and after for each element I check if it is a string (otherwise I cast it) and I append to variable serialized.

    The issue is delete doesn't work. I get this error "wrong number of arguments (0 for 1)". What's wrong ??? I mean self.attributes is a hash and delete["key"] should delete the element.


    Moreover is it possible to ordinate the hash elements in according to the database schema ??? for instance if the schema is name,color,shape I would like to have name first hash element, color second and shape third even if I submit them on different order.

    THANK YOU
    Alla prossima ...

  2. #2
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the error is because you need to use () rather that [] brackets - ie:

    self.attributes.delete('checksum')

    Only issue I see with that, is that will permanently delete the attribute for the life of the object, so you will need to refresh the object if you need to access it. Why don't you just do this:

    Code:
    serialized = ""
    self.attributes.each do |key,value|
        if key != "checksum"
            if value.is_a?(String)   
                serialized += value
            else
                serialized += value.to_s
            end
        end
     end

  3. #3
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi pilot
    yes it is the solution I found anyway I guess I will not need checksum within this method.


    Don't you have any idea about ordinating the hash elements in according to the database schema ???


    THANKS and HAVE A GOOD 1 !!!
    Alla prossima ...

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by macsig View Post
    hi pilot
    yes it is the solution I found anyway I guess I will not need checksum within this method.


    Don't you have any idea about ordinating the hash elements in according to the database schema ???


    THANKS and HAVE A GOOD 1 !!!
    Hash keys aren't meant to be ordered like that, nor should it really matter to your database.

  5. #5
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know, but I need to find a way to order them.

    And how about using alphabetic key's order ???
    Alla prossima ...

  6. #6
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code Ruby:
    hash.sort_by(&:first)

  7. #7
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does hash#sort_by return an array of arrays like hash#sort does? Something to be aware of (if you want an ordered collection you shouldn't really be using a hash hence the above behaviour).

  8. #8
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need to serialize the values in the attributes hash and I need to be sure that they are always on the same order so I need to order them in some way.
    Alla prossima ...

  9. #9
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Luke, yes it returns an array of pairs of keys and values.

    Example serialize:

    Code Ruby:
    hash.sort_by(&:first).map{|(k,v)| "#{k}: #{v}"}.join("\n")

  10. #10
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    Luke, yes it returns an array of pairs of keys and values.

    Example serialize:

    Code Ruby:
    hash.sort_by(&:first).map{|(k,v)| "#{k}: #{v}"}.join("\n")
    Thank you I'm gonna try it
    Alla prossima ...

  11. #11
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Fenrir, I'm trying to use your code but I don't understand in which way works. I mean what should it return, for instance what i should have on serialized after the execution of this line:
    Code:
    serialized = self.attributes.sort_by(&:first).map{|(k,v)| "#{k}: #{v}"}.join("\n")
    THANK YOU and HAVE A NICE DAY!
    Alla prossima ...

  12. #12
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For example:

    Code:
    {"a" => "foo",
     "b" => "bar",
     "c" => "quux"}
    then the serialized version of this data is:
    Code:
    a: foo
    b: bar
    c: quux
    Note that the code is untested, so the result you get may be different.

    Why do you want to serialize the objects? Are you aware of yaml?

    Code Ruby:
    ~$ irb
    irb(main):001:0> require 'yaml'
    => true
    irb(main):002:0> serialized = YAML.dump({"a" => "foo", "b" => "bar", "c" => "quux"})
    => "--- \na: foo\nb: bar\nc: quux\n"
    irb(main):003:0> puts serialized
    --- 
    a: foo
    b: bar
    c: quux
    => nil
    irb(main):004:0> YAML.load(serialized)
    => {"a"=>"foo", "b"=>"bar", "c"=>"quux"}
    irb(main):005:0>

  13. #13
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the example
    So if I would like, starting from

    Code:
    {"a" => "foo",
     "b" => "bar",
     "c" => "quux"}
    obtaining something like
    Code:
    serialized="foobarquux"
    I should use
    Code:
    serialized = self.attributes.sort_by(&:first).map{|(k,v)| "# #{v}"}
    Isn't it ???

    last question using sort_by(&:first).map will I obtain serialized="foobarquux even if my hash is { "b" => "bar","a" => "foo", "c" => "quux" } ???

    THANK YOU
    Alla prossima ...

  14. #14
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep. Play with the code to change the formatting of the serialized data.

    BTW, are you planning to deserialize the data? I don't think it's possible if you store it like this...

  15. #15
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need serialized string to create a checksum in order to know if the data is changed outside my application.

    I don't need to deserialize them.


    actually I solved my problem with the code below (I know it is not "graceful" but works)
    Code:
         serialized = ""
          
          myKeys =  self.attributes.keys
          myKeys.delete("checksum")
          mySortedKeys = myKeys.sort
          
         mySortedKeys.each do |key| 
           
            if self.attributes[key].is_a?(String)   
                      serialized += self.attributes[key]
                  else
                      serialized += self.attributes[key].to_s
             end
           end
    THANK YOU ONCE AGAIN
    Last edited by macsig; Oct 22, 2007 at 22:24. Reason: Update new code
    Alla prossima ...

  16. #16
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wouldn't you use an updated_at field for this?

  17. #17
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    Wouldn't you use an updated_at field for this?
    Sorry but I don't see in which way updated_at could help me to achieve my goal.
    Alla prossima ...

  18. #18
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to know if the data is changed, right? Well, the updated_at field changes every time you change your model, so it can act like a checksum.

  19. #19
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    You need to know if the data is changed, right? Well, the updated_at field changes every time you change your model, so it can act like a checksum.
    Sorry but if, for instance, I have a simple model "warehouse" with 2 attributes: name and status and through the application I input this warehouse:

    name = "MyWarehouse"
    status="0"

    if I have updated_at this value is automatically set as "10/23/2007" isn't it ???

    Now if someone who can access to the DB changes manually status="1"; in which way can I know that looking at updated_at ???


    THANKS
    Alla prossima ...

  20. #20
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by macsig View Post
    Now if someone who can access to the DB changes manually status="1"; in which way can I know that looking at updated_at ???
    The problem here is that you are letting people bypass your domain model and access your db directly; why do it? If people need to get data from your DB directly, give them read only access and provide an API for interface for those who want to change data.

  21. #21
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Luke Redpath View Post
    The problem here is that you are letting people bypass your domain model and access your db directly; why do it? If people need to get data from your DB directly, give them read only access and provide an API for interface for those who want to change data.
    I don't let them bypass the domain model and access to the DB directly; but once the application will be installed on my customer's server, the system admin will have control of the DB. So I need to be sure that any change can be notified.

    Moreover a checksum system is required to be CFR 21 part 11 compliant


    Have a nice day!
    Alla prossima ...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •