byroot / activerecord-typedstore

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

Update a typed hstore #11

Closed Bahanix closed 10 years ago

Bahanix commented 10 years ago
# 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.

byroot commented 10 years ago

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+

Bahanix commented 10 years ago

Yes: rails 4.0.2 pg 0.17.1 activerecord-typedstore 0.4.2

byroot commented 10 years ago

That's really weird. It works in my tests: https://github.com/byroot/activerecord-typedstore/blob/a93f48548d9a451e1b281875f0cb13966fa200eb/spec/active_record/typed_store_spec.rb#L630

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.

Bahanix commented 10 years ago

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

Thank you!

byroot commented 10 years ago

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

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: https://github.com/byroot/activerecord-typedstore/blob/a93f48548d9a451e1b281875f0cb13966fa200eb/spec/support/models.rb#L143

byroot commented 10 years ago

@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!

Bahanix commented 10 years ago

It works, thank you very much!