views:

37

answers:

2

Hi,

I have the following object that has been created

@post = Post.create(:name => 'test', :post_number => 20, :active => true)

Once this is saved, I want to be able to get the object back to a hash, e.g. by doing somthing like:

@object.to_hash

How is this possible from within rails?

Thanks

+3  A: 

If you are looking for only attributes, then you can get them by:

@post.attributes
Swanand
A: 

You could definitely use the attributes to return all attributes but you could add an instance method to Post, call it "to_hash" and have it return the data you would like in a hash. Something like

def to_hash; {:name => self.name, :active = true}; end

and so on.

Curtis Edmond