serialize ActiveRecord::Coders::Hstore crashes RailsAdmin

霸气de小男生 提交于 2020-01-24 00:35:33

问题


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):

<% @item.properties.try(:each) do |key, value| %>
  <%= key %>: <%= value %><br />
<% end %>

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)



来源:https://stackoverflow.com/questions/14914543/serialize-activerecordcodershstore-crashes-railsadmin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!