# Object to hash

**URL:** <https://www.ruby-forum.com/t/object-to-hash/131133>\
**Category:** Rails\
**Created:** [March 6, 2008, 10:51am UTC](https://www.ruby-forum.com/t/object-to-hash/131133 "2008-03-06T10:51:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel304](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@daniel304](https://www.ruby-forum.com/u/daniel304)\
**Post date:** [March 6, 2008, 10:51am UTC](https://www.ruby-forum.com/t/object-to-hash/131133/1 "2008-03-06T10:51:44Z")

</div>

I am working with rails 2.0.2 and ActiveResource

I have an object @person and I need to save it locally and send the same  
object with Active Resource to a Restfull server…  
My solution to this is to define a  
@person.new(params[:person]) and a RestFullPerson hooked on  
ActiveResource::Base  
the problem is when I do a save of the Restfull person it need a hash  
structure  
so I can’t do the following:

result = RestfullPerson.new(@person)

I can’t get any to\_hash methods to work. any ideas are welcome

---

<div class="post-metadata">

**Author:** ![daniel304](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@daniel304](https://www.ruby-forum.com/u/daniel304)\
**Post date:** [March 6, 2008, 2:55pm UTC](https://www.ruby-forum.com/t/object-to-hash/131133/2 "2008-03-06T14:55:45Z")

</div>

On 6 Mar 2008, at 09:51, Daniel Van den oord wrote:

> structure  
> so I can’t do the following:
> 
> result = RestfullPerson.new(@person)
> 
> I can’t get any to\_hash methods to work. any ideas are welcome

I’m not 100% sure i’ve understood your question, but is  
@person.attributes what you’re after ?

Fred

---

<div class="post-metadata">

**Author:** ![daniel304](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@daniel304](https://www.ruby-forum.com/u/daniel304)\
**Post date:** [December 15, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/object-to-hash/131133/3 "2008-12-15T15:21:04Z")

</div>

Frederick C. wrote:

> On 6 Mar 2008, at 09:51, Daniel Van den oord wrote:
> 
> > structure  
> > so I can’t do the following:
> > 
> > result = RestfullPerson.new(@person)
> > 
> > I can’t get any to\_hash methods to work. any ideas are welcome
> 
> I’m not 100% sure i’ve understood your question, but is  
> @person.attributes what you’re after ?
> 
> Fred

Thanks Fred.

This is exactly what I was looking for. You might also want to point  
out that you can also do this

@person.attributes.to\_options!

That will symbolize the keys, to make it look like the params[] field  
that is passed to the controller

Kind Regards  
Hamza
