Permalink
Browse files

can create hstore records via AR API

  • Loading branch information...
1 parent 135b3a0 commit 96838b5192e46caecc73cf45968fd27b78a41404 @tenderlove tenderlove committed Dec 20, 2011
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##
+* PostgreSQL hstore records can be created.
+
* PostgreSQL hstore types are automatically deserialized from the database.
## Rails 3.2.0 (unreleased) ##
@@ -50,11 +50,15 @@ def string_to_time(string)
end
end
- def cast_hstore(string)
- kvs = string.split(', ').map { |kv|
- kv.split('=>').map { |k| k[1...-1] }
- }
- Hash[kvs]
+ def cast_hstore(object)
+ if Hash === object
+ object.map { |k,v| "#{k}=>#{v}" }.join ', '
+ else
+ kvs = object.split(', ').map { |kv|
+ kv.split('=>').map { |k| k[1...-1] }
+ }
+ Hash[kvs]
+ end
end
end
# :startdoc:
@@ -43,4 +43,11 @@ def test_select_multikey
x = Hstore.find :first
assert_equal({'1' => '2', '2' => '3'}, x.tags)
end
+
+ def test_create
+ hash = { 'a' => 'b', '1' => '2' }
+ x = Hstore.create!(:tags => hash)
+ x.reload
+ assert_equal hash, x.tags
+ end
end

0 comments on commit 96838b5

Please sign in to comment.