diff options
author | Yves Senn <yves.senn@gmail.com> | 2013-10-25 08:17:40 -0700 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2013-10-25 08:17:40 -0700 |
commit | dc8fac1cacb120dc2535b67bdd6436d220901c7a (patch) | |
tree | 0949275d6b2bc9827ab9a35b88575cbb423f8456 /activerecord | |
parent | 0e918ea97acc71e23e7a43963a5147d0ad8a994d (diff) | |
parent | c3606afb2a54ac11e31360cf1dbe13fbdf893f73 (diff) | |
download | rails-dc8fac1cacb120dc2535b67bdd6436d220901c7a.tar.gz rails-dc8fac1cacb120dc2535b67bdd6436d220901c7a.tar.bz2 rails-dc8fac1cacb120dc2535b67bdd6436d220901c7a.zip |
Merge pull request #12643 from severin/pg_cast_json_on_write
cast json values on write to be consistent with reading from the db.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 15 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql/oid.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/adapters/postgresql/json_test.rb | 7 |
3 files changed, 26 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 7062d59a3d..0c325900e8 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,18 @@ +* Type cast json values on write, so that the value is consistent + with reading from the database. + + Example: + + x = JsonDataType.new tags: {"string" => "foo", :symbol => :bar} + + # Before: + x.tags # => {"string" => "foo", :symbol => :bar} + + # After: + x.tags # => {"string" => "foo", "symbol" => "bar"} + + *Severin Schoepke* + * `ActiveRecord::Store` works together with PG `hstore` columns. Fixes #12452. diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb index 4babee07b4..6c5792954f 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb @@ -249,6 +249,10 @@ module ActiveRecord end class Json < Type + def type_cast_for_write(value) + ConnectionAdapters::PostgreSQLColumn.json_to_string value + end + def type_cast(value) return if value.nil? diff --git a/activerecord/test/cases/adapters/postgresql/json_test.rb b/activerecord/test/cases/adapters/postgresql/json_test.rb index f7609404ce..c33c7ef968 100644 --- a/activerecord/test/cases/adapters/postgresql/json_test.rb +++ b/activerecord/test/cases/adapters/postgresql/json_test.rb @@ -49,6 +49,13 @@ class PostgresqlJSONTest < ActiveRecord::TestCase JsonDataType.reset_column_information end + def test_cast_value_on_write + x = JsonDataType.new payload: {"string" => "foo", :symbol => :bar} + assert_equal({"string" => "foo", "symbol" => "bar"}, x.payload) + x.save + assert_equal({"string" => "foo", "symbol" => "bar"}, x.reload.payload) + end + def test_type_cast_json assert @column |