# App works but test/unit fail w/ hash (undefined method 'each

**URL:** <https://www.ruby-forum.com/t/app-works-but-test-unit-fail-w-hash-undefined-method-each/106231>\
**Category:** Ruby\
**Created:** [June 7, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/app-works-but-test-unit-fail-w-hash-undefined-method-each/106231 "2007-06-07T18:40:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Toddnix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ad7895/32.png) [@Toddnix](https://www.ruby-forum.com/u/Toddnix)\
**Post date:** [June 7, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/app-works-but-test-unit-fail-w-hash-undefined-method-each/106231/1 "2007-06-07T18:40:00Z")

</div>

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 .save method is only called once in the  
test, it’s actually be called twice in test/unit.

Any thoughts are much appreciated.

---

<div class="post-metadata">

**Author:** ![Toddnix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ad7895/32.png) [@Toddnix](https://www.ruby-forum.com/u/Toddnix)\
**Post date:** [June 7, 2007, 7:12pm UTC](https://www.ruby-forum.com/t/app-works-but-test-unit-fail-w-hash-undefined-method-each/106231/2 "2007-06-07T19:12:02Z")

</div>

Hi Toddnix,

forgot the `serialize :domains, Hash’ ?

Florian

Am 07.06.2007 um 18:30 schrieb Toddnix:
