Espen Antonsen
2012-Jun-01 12:56 UTC
Serialized attribute saved as HashWithIndifferentAccess in database
My Booking model has: serialize :custom_data, Hash From the console it works as expected saving values to the custom_data attribute. But when having a form with parameters like this: booking[custom_data][hello] and creating a new object in the controller like this: Booking.new( params[:booking] ), values are saved in the database with added metadata like this: --- !ruby/hash:ActiveSupport::HashWithIndifferentAccess How can get rid of the HashWithIndifferentAccess-metadata when saving from a form? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/1WO6HkqIqjAJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
murali dhararao
2012-Jun-01 12:59 UTC
Re: Serialized attribute saved as HashWithIndifferentAccess in database
Hi, You can check condition like this if params[:booking].kind_of? HashWithIndifferentAccess // logic here end On Fri, Jun 1, 2012 at 6:26 PM, Espen Antonsen <espen-ujxtt+JBbP4XWF+eFR7m5Q@public.gmane.org> wrote:> My Booking model has: serialize :custom_data, Hash > > From the console it works as expected saving values to the custom_data > attribute. > > But when having a form with parameters like > this: booking[custom_data][hello] and creating a new object in the > controller like this: Booking.new( params[:booking] ), values are saved in > the database with added metadata like this: --- > !ruby/hash:ActiveSupport::HashWithIndifferentAccess > > How can get rid of the HashWithIndifferentAccess-metadata when saving from > a form? > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/1WO6HkqIqjAJ. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- Thanks & Regards, MuraliDharaRao.T +91-9642234646 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Espen Antonsen
2012-Jun-01 13:15 UTC
Re: Serialized attribute saved as HashWithIndifferentAccess in database
I fixed it by converting it to a hash: @booking.custom_data = params[:booking][:custom_data].to_hash if params[:booking][:custom_data] I do think that Rails should handle this automatically though. There is no way to see this error by just using rails. You have to look at the actual data stored in the database to see that it stores more than just the actual data. I would suspect there are more people saving serialised attributes from a form and will have this issue without realising it. On Friday, June 1, 2012 2:56:13 PM UTC+2, Espen Antonsen wrote:> > My Booking model has: serialize :custom_data, Hash > > From the console it works as expected saving values to the custom_data > attribute. > > But when having a form with parameters like > this: booking[custom_data][hello] and creating a new object in the > controller like this: Booking.new( params[:booking] ), values are saved in > the database with added metadata like this: --- > !ruby/hash:ActiveSupport::HashWithIndifferentAccess > > How can get rid of the HashWithIndifferentAccess-metadata when saving from > a form? >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/JNiKJRng5rUJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Matt Jones
2012-Jun-02 15:26 UTC
Re: Serialized attribute saved as HashWithIndifferentAccess in database
On Friday, 1 June 2012 09:15:18 UTC-4, Espen Antonsen wrote:> > I fixed it by converting it to a hash: > @booking.custom_data = params[:booking][:custom_data].to_hash if > params[:booking][:custom_data] > > I do think that Rails should handle this automatically though. There is no > way to see this error by just using rails. You have to look at the actual > data stored in the database to see that it stores more than just the actual > data. I would suspect there are more people saving serialised attributes > from a form and will have this issue without realising it. > >I don''t follow that logic at all. If you''re interacting with a serialized column by any other means than loading it from the DB as a Ruby object, you are DOING IT WRONG. If you really, really want to interact with hashes in the DB, check out things like Postgres''s Hstore: http://www.postgresql.org/docs/9.0/static/hstore.html --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/yjHWHY-ln4wJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.