1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

cast json values on write to be consistent with reading from the db.

See also commit 5ac2341fab
This commit is contained in:
Severin Schoepke 2013-10-25 14:45:52 +02:00
parent 3c3ffac011
commit c3606afb2a
3 changed files with 26 additions and 0 deletions

View file

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

View file

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

View file

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