madeintandem / hstore_accessor

Adds typed hstore-backed field support to ActiveRecord models.
MIT License
242 stars 47 forks source link

support array of hstores #14

Closed ramigg closed 9 years ago

ramigg commented 10 years ago

as of this commit https://github.com/rails/rails/commit/ad84512f89b4da69d787f2574a3a5e93945005b5 rails supports arrays of hstores It would be very useful to add this support here.

thegrubbsian commented 10 years ago

I'm not sure what the use case is here, is this just shoving hashes in an array field? Can you give me a concrete example of how you'd like to use this?

pallymore commented 10 years ago

I ran into this issue recently as well. I'm using the hstore field to store some customization settings for our pdf report generator, one of them being able to customize how the labels show up in the page, eg:

[
  {
    name: 'Dave',
    initials: 'DS',
    alias: 'David'
}, {
    #....etc. 
}
]

well this setting only applies to a small portion of our customers (for now) that's why we are putting it inside the hstore, along with other various custom settings.

currently when it's saved, the hashes become strings like:

{\"name\"=>\"Dave\"}

which cannot be parsed back to hash (by using JSON.parse) because of the '=>'s in the string. :)

milankubin commented 10 years ago

I also have need for this. In my use case I'm using paper_trail to version a model. It has some properties like a collection of addresses. paper_trail doesn't allow versioning of has_many and belongs_to associations.

@pallymore You can use OpenStruct.new(object) to parse back to a hash in ruby.

 - unless resource.address.first["position"].blank?
                    - blob = resource.address.sort_by{ |k| k["position"] }
                    - blob.each do |address| 
                      = f.fields_for :address, OpenStruct.new(address), index: "" do |builder|
                        = render partial: 'address_fields', locals: { t: builder, a: OpenStruct.new(address)}

(a will allow me to access the objects value's ) Note the lack of index

t.text_field :street ...

You can build an empty fieldset as so

 = f.fields_for 'address[]', OpenStruct.new({:address => [ :city, :street, :street_no, :street_no_add, :zip, :position]}), index: "" do |builder|

and for strong_parameters

  def permitted_params
   params.require(:address).permit(:lead => 
   [
    :address => [
      :city, :street, :street_no, :street_no_add, :zip, :position  
      ],
    :m3_data => [
      :name, :weight, :qty
      ],
   ])
  end

I've done some hackery in regards to hstore array's because this is a need for me as well. Came across this gem, but alas no one seems to appreciate any use case for hstore array of hashes. I'm going to dive into this gem a little and see what we can do.

tonycoco commented 9 years ago

This is being fixed in a possible 2.0.0 change -- See: #40