App works but test/unit fail w/ hash (undefined method 'each_key' for :String)

My app takes an account_type from a reference table when creating user
accounts and, when the obj is saved the model the numeric key for the
account_type is used for a lookup to get a user role which is then put
into a hash (used for app authorization). Before actually saving into
the DB the model converts the hash into a string. The following code
shows the conversion method:

.
.
before_save :hash_domains
.
.
def hash_domains
  #first get the domain name from the ref_tables via the key
  @ref = RefTable.find[:first,
      :conditions => ["key_name = ?", self.account_type])

  if self.domains.length > 0 #the domains hash exists
    self.domains.each_key {|dom| #<- HERE IS WHERE IT BOMBS OUT!!
        if not dom == "ADMIN"
            self.domains.delete(dom)
            self.domains[@ref.key_desc] = '1'
        end
    }
  else
    self.domains = {@ref.key_desc => '1'}
  end
  write_attribute "domains", User.domain2str(self.domains)
end

Obviously when test/unit runs the domains obj is a string and doesn't
have the each_key method. What is killing me is the app WORKS with
identical data when a record is inserted/created. Also, oddly the
error from test/unit includes a correctly formatted 'domains' obj
string, which shouldn't exist until the last line. This makes me think
that even though the <model>.save method is only called once in the
test, it's actually be called twice in test/unit.

Any thoughts are much appreciated.

Hi Toddnix,

forgot the `serialize :domains, Hash' ?

Florian

···

Am 07.06.2007 um 18:30 schrieb Toddnix:

My app takes an account_type from a reference table when creating user
accounts and, when the obj is saved the model the numeric key for the
account_type is used for a lookup to get a user role which is then put
into a hash (used for app authorization). Before actually saving into
the DB the model converts the hash into a string. The following code
shows the conversion method:

.
before_save :hash_domains
.
def hash_domains
  #first get the domain name from the ref_tables via the key
  @ref = RefTable.find[:first,
      :conditions => ["key_name = ?", self.account_type])

  if self.domains.length > 0 #the domains hash exists
    self.domains.each_key {|dom| #<- HERE IS WHERE IT BOMBS OUT!!
        if not dom == "ADMIN"
            self.domains.delete(dom)
            self.domains[@ref.key_desc] = '1'
        end
    }
  else
    self.domains = {@ref.key_desc => '1'}
  end
  write_attribute "domains", User.domain2str(self.domains)
end

Obviously when test/unit runs the domains obj is a string and doesn't
have the each_key method. What is killing me is the app WORKS with
identical data when a record is inserted/created. Also, oddly the
error from test/unit includes a correctly formatted 'domains' obj
string, which shouldn't exist until the last line. This makes me think
that even though the <model>.save method is only called once in the
test, it's actually be called twice in test/unit.

Any thoughts are much appreciated.