solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 228 forks source link

How can I use array of tags(Strings) in a key of a hash? #392

Closed rafayet-monon closed 5 years ago

rafayet-monon commented 5 years ago

This is a question.

I am saving a hash in a postgres column using hstore. Then serializing the column in User model

serialize :dating_preferences, UserDatingPreferences

The UserDatingPreferences class is below, there are other attributes as well besides denominations. I just posted denominations here -

class UserDatingPreferences
  include Virtus.model

  attribute :denominations, Array[String], default: []

  def self.dump(preferences)
    preferences.to_hash
  end

  def self.load(preferences)
    new(preferences)
  end
end

It shows dating response like below, array of string and then another array in string

"dating_preferences": {
   "denominations": [
        "[]"
    ]
 }

After saving it like

    user.dating_preferences.denominations = ['u00o','yo']
    user.save!

The response looks like

"dating_preferences": {
            "denominations": [
                "[\"u00o\", \"yo\"]"
            ]
        }

Can it be made like below ?

"dating_preferences": {
            "denominations": [ "u00o", "yo"]
        }
rafayet-monon commented 5 years ago

Apparently postgresql hstore doesn't work that way, didn't find why but using jsonb solved my problems and got what I expected.