{"id":678,"date":"2022-08-30T15:05:21","date_gmt":"2022-08-30T15:05:21","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/serialize-activerecordcodershstore-crashes-railsadmin-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:05:21","modified_gmt":"2022-08-30T15:05:21","slug":"serialize-activerecordcodershstore-crashes-railsadmin-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/serialize-activerecordcodershstore-crashes-railsadmin-collection-of-common-programming-errors\/","title":{"rendered":"serialize ActiveRecord::Coders::Hstore crashes RailsAdmin-Collection of common programming errors"},"content":{"rendered":"<p>As suggested in http:\/\/railscasts.com\/episodes\/345-hstore, I added:<\/p>\n<pre><code>serialize :properties, ActiveRecord::Coders::Hstore\n<\/code><\/pre>\n<p>to my model. This allows me to iterate over key \/ value pairs in the view (without it, I get a undefined method `each&#8217; for String error):<\/p>\n<pre><code>\n  : <br \/>\n\n<\/code><\/pre>\n<p>I managed to get the hstore field in RailsAdmin working by explicitly declaring it as a text field:<\/p>\n<pre><code>class Item &lt; ActiveRecord::Base\n  attr_accessible :name, :properties, as: :admin\n\n  serialize :properties, ActiveRecord::Coders::Hstore  \n\n  rails_admin do # without this block, properties gets ignored in RailsAdmin\n    edit do\n      field :name\n      field :properties, :text\n    end\n  end\nend\n<\/code><\/pre>\n<p>If I have the serialize call above deactivated, I can set hashes in RailsAdmin in the following format:<\/p>\n<pre><code>\"key1\" =&gt; \"value1\", \"key2\" =&gt; \"value2\"\n<\/code><\/pre>\n<p>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:<\/p>\n<pre><code>undefined method `map' for String\n<\/code><\/pre>\n<p>What to do?<\/p>\n<ol>\n<li>\n<p>I believe you can do simply this:<\/p>\n<pre><code>serialize :properties\n<\/code><\/pre>\n<p>This should work with <code>each<\/code> method. Then :properties will be recognized as hash, not string.<\/p>\n<p>HStore should be optional.<\/p>\n<p>:properties should be String in your schema<br \/>\n(I think when you&#8217;re setting up HStore plugin &#8211; you put hstore type instead of string)<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:26:57. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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&#8217; for String error): : I managed to get the hstore field in RailsAdmin working by explicitly declaring it as a text field: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-678","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=678"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/678\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}