byroot/activerecord-typedstore

Update a typed hstore

Bahanix opened this issue · 7 comments

# app/models/profile.rb
class Profile < ActiveRecord::Base
  typed_store :properties do |p|
    p.boolean :smoker
  end
end

# irb
Profile.last.update properties: {smoker: false}
# or
Profile.last.update smoker: false
# or
p = Profile.last
p.smoker = false
p.save
# or
p = Profile.last
p.properties = {smoker: false}
p.save
 => UPDATE "profiles" SET "properties" = $1, "updated_at" = $2 WHERE "profiles"."id" = 1  [["properties", "--- !ruby/hash:Profile::PropertiesHash\nsmoker: false\n"], ["updated_at", Sat, 15 Feb 2014 09:35:48 UTC +00:00]]
PG::InternalError: ERREUR:  Syntax error near '!' at position 4

Profile#properties is a PG hstore. If I remove the typed_store block from model, all seems to work well. Did I do something wrong?

Here typed_store seems perfect to retreive data but not to set data.

The same at activerecord-typedstore master.

Interesting, are you on Rails 4 ?

I should have stated it explicitly in the readme, but HStore and JSON columns are only with Rails 4+

Yes:
rails 4.0.2
pg 0.17.1
activerecord-typedstore 0.4.2

That's really weird. It works in my tests:

describe PostgresHstoreTypedStoreModel do

I'll try to figure out what's going on. In the meantime if you could provide a minimal app that trigger the issue, that would help a lot. In any case I'll do my best to fix that ASAP.

Here's the minimal app:
https://github.com/Bahanix/typed-hstore

Thank you!

@Bahanix thanks a lot. Actually HStore was not properly tested because of a broken condition in my test suite 😭

I now have a failing test suite: #12.
I have to flee to work now, but be certain that I'll fix that as soon as I get a couple hours ahead of me.

In the meantime, I would recommend to use the JSON type instead, if it suits your needs.
Here's an example of how to use it:

class PostgresJsonTypedStoreModel < ActiveRecord::Base

@Bahanix so I found the issue.

HStore serilization like for JSON columns is handled at the driver level, so you need to define a custom identity coder.

I added a REAME section about that: https://github.com/byroot/activerecord-typedstore#serialization-methods

I'm really sorry that it took so long, I should have written that readme entry a while ago.

In the future I'll see what I can do to detect HStore and JSON columns to prive a default coder.

Thanks for using typed-store!

It works, thank you very much!