serialize ActiveRecord::Coders::Hstore crashes RailsAdmin-Collection of common programming errors

As suggested in http://railscasts.com/episodes/345-hstore, I added:

serialize :properties, ActiveRecord::Coders::Hstore

to my model. This allows me to iterate over key / value pairs in the view (without it, I get a undefined method `each’ for String error):


  : 

I managed to get the hstore field in RailsAdmin working by explicitly declaring it as a text field:

class Item < ActiveRecord::Base
  attr_accessible :name, :properties, as: :admin

  serialize :properties, ActiveRecord::Coders::Hstore  

  rails_admin do # without this block, properties gets ignored in RailsAdmin
    edit do
      field :name
      field :properties, :text
    end
  end
end

If I have the serialize call above deactivated, I can set hashes in RailsAdmin in the following format:

"key1" => "value1", "key2" => "value2"

However, when the serialize call is not commented out (in order for the loop in the view to work), RailsAdmin crashes when saving the Hash:

undefined method `map' for String

What to do?

  1. I believe you can do simply this:

    serialize :properties
    

    This should work with each method. Then :properties will be recognized as hash, not string.

    HStore should be optional.

    :properties should be String in your schema
    (I think when you’re setting up HStore plugin – you put hstore type instead of string)

Originally posted 2013-11-09 21:26:57.