byroot / activerecord-typedstore

ActiveRecord::Store but with type definition
MIT License
439 stars 57 forks source link

typed_store arrays remain empty (using hstore) #13

Closed Bahanix closed 10 years ago

Bahanix commented 10 years ago

While boolean, string, datetime and integer work very well in a typed hstore, arrays remain empty:

class Dog < ActiveRecord::Base
  typed_store :properties, coder: DumbCoder do |p|
    p.boolean :is_fat
    p.string :toys, array: true, default: [], null: false
  end
end
Dog.create is_fat: true, toys: ['bone']
 => INSERT INTO "dogs" ("created_at", "properties", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["created_at", Tue, 18 Feb 2014 11:21:32 UTC +00:00], ["properties", {"toys"=>["bone"], "is_fat"=>true}}], ["updated_at", Tue, 18 Feb 2014 11:21:32 UTC +00:00]]
 => (0.5ms)  COMMIT
 => #<Dog id: 2, name: nil, properties: {"toys"=>["bone"], "is_fat"=>true}, created_at: "2014-02-18 11:21:32", updated_at: "2014-02-18 11:21:32">

Dog.last
 => Dog Load (0.9ms)  SELECT "dogs".* FROM "dogs" ORDER BY "dogs"."id" DESC LIMIT 1
 => #<Dog id: 2, name: nil, properties: {"toys"=>[], "is_fat"=>true}, created_at: "2014-02-18 11:21:32", updated_at: "2014-02-18 11:21:32">

Note: the empty array only appear after retrieving data, while the returned Dog instance on create and update is OK.

Here's the minimal app reproducing this issue: https://github.com/Bahanix/typed-hstore/commit/c836018ec661a8ed8b181fa9cbe1c07d0999080b

Thank for your work!

byroot commented 10 years ago

Damn... Arrays are not supported with HStore. Because HStore store everything as a string. So It would mean storing some YAML or other format inside the HStore which IMO defeat the purpose.

I will add it to the README.

Note that it may change with future Postgres releases, they are talking about typed hstores.

But it does works very well with JSON....

Really sorry...

Bahanix commented 10 years ago

I understand, sorry for the inconvenience! JSON does not fit my need to query efficiently the store (using the @> postgres hstore operator). I'll think about a solution. Thanks for your work.

byroot commented 10 years ago

No inconvenience here. You forced me to be more explicit in the readme. That's all good!