Forum: Ruby on Rails Serialized attribute saved as HashWithIndifferentAccess in database

De52481f089fd3ba8d82cf8ef2329476?d=identicon&s=25 Espen Antonsen (Guest)
on 2012-06-01 14:57
(Received via mailing list)
My Booking model has: serialize :custom_data, Hash

From the console it works as expected saving values to the custom_data

But when having a form with parameters like
this: booking[custom_data][hello] and creating a new object in the
controller like this: params[:booking] ), values are saved
the database with added metadata like this: ---

How can get rid of the HashWithIndifferentAccess-metadata when saving
a form?
557321e6903cf5ec6a9b4f22e1ef3ad2?d=identicon&s=25 Murali Tirupati (sumudhar)
on 2012-06-01 15:13
(Received via mailing list)

You can check condition like this

 if params[:booking].kind_of? HashWithIndifferentAccess
  // logic here

On Fri, Jun 1, 2012 at 6:26 PM, Espen Antonsen <>

> For more options, visit this group at

Thanks & Regards,
De52481f089fd3ba8d82cf8ef2329476?d=identicon&s=25 Espen Antonsen (Guest)
on 2012-06-01 15:16
(Received via mailing list)
I fixed it by converting it to a hash:
@booking.custom_data = params[:booking][:custom_data].to_hash if

I do think that Rails should handle this automatically though. There is
way to see this error by just using rails. You have to look at the
data stored in the database to see that it stores more than just the
data. I would suspect there are more people saving serialised attributes
from a form and will have this issue without realising it.
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2012-06-02 17:27
(Received via mailing list)
On Friday, 1 June 2012 09:15:18 UTC-4, Espen Antonsen wrote:
I don't follow that logic at all. If you're interacting with a
column by any other means than loading it from the DB as a Ruby object,
are DOING IT WRONG. If you really, really want to interact with hashes
the DB, check out things like Postgres's Hstore:

--Matt Jones
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.