File tree 4 files changed +34
-4
lines changed
lib/active_record/connection_adapters
test/cases/adapters/postgresql
4 files changed +34
-4
lines changed Original file line number Diff line number Diff line change
1
+ * Type cast hstore values on write, so that the value is consistent
2
+ with reading from the database.
3
+
4
+ Example:
5
+
6
+ x = Hstore.new tags: {"bool" => true, "number" => 5}
7
+
8
+ # Before:
9
+ x.tags # => {"bool" => true, "number" => 5}
10
+
11
+ # After:
12
+ x.tags # => {"bool" => "true", "number" => "5"}
13
+
14
+ * Yves Senn* , * Severin Schoepke*
15
+
1
16
* Fix multidimensional PG arrays containing non-string items.
2
17
3
18
* Yves Senn*
Original file line number Diff line number Diff line change @@ -6,10 +6,6 @@ class PostgreSQLAdapter < AbstractAdapter
6
6
module OID
7
7
class Type
8
8
def type ; end
9
-
10
- def type_cast_for_write ( value )
11
- value
12
- end
13
9
end
14
10
15
11
class Identity < Type
@@ -224,6 +220,10 @@ def type_cast(value)
224
220
end
225
221
226
222
class Hstore < Type
223
+ def type_cast_for_write ( value )
224
+ ConnectionAdapters ::PostgreSQLColumn . hstore_to_string value
225
+ end
226
+
227
227
def type_cast ( value )
228
228
return if value . nil?
229
229
Original file line number Diff line number Diff line change @@ -129,6 +129,14 @@ def self.extract_value_from_default(default)
129
129
end
130
130
end
131
131
132
+ def type_cast_for_write ( value )
133
+ if @oid_type . respond_to? ( :type_cast_for_write )
134
+ @oid_type . type_cast_for_write ( value )
135
+ else
136
+ super
137
+ end
138
+ end
139
+
132
140
def type_cast ( value )
133
141
return if value . nil?
134
142
return super if encoded?
Original file line number Diff line number Diff line change @@ -70,6 +70,13 @@ def test_change_table_supports_hstore
70
70
Hstore . reset_column_information
71
71
end
72
72
73
+ def test_cast_value_on_write
74
+ x = Hstore . new tags : { "bool" => true , "number" => 5 }
75
+ assert_equal ( { "bool" => "true" , "number" => "5" } , x . tags )
76
+ x . save
77
+ assert_equal ( { "bool" => "true" , "number" => "5" } , x . reload . tags )
78
+ end
79
+
73
80
def test_type_cast_hstore
74
81
assert @column
75
82
You can’t perform that action at this time.
0 commit comments