mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #12643 from severin/pg_cast_json_on_write
cast json values on write to be consistent with reading from the db.
This commit is contained in:
commit
dc8fac1cac
3 changed files with 26 additions and 0 deletions
|
@ -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.
|
||||
|
||||
|
|
|
@ -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?
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue