Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
can create hstore records via AR API
  • Loading branch information
tenderlove committed Dec 20, 2011
1 parent 135b3a0 commit 96838b5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG.md
@@ -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) ##
Expand Down
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/adapters/postgresql/hstore_test.rb
Expand Up @@ -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.