@@ -109,12 +109,16 @@ def find_by_id(id, options = {})
109
109
def _find_all ( ids , options = { } )
110
110
raise Errors ::MissingRangeKey if range_key && ids . any? { |_pk , sk | sk . nil? }
111
111
112
- if range_key
113
- ids = ids . map do |pk , sk |
114
- sk_casted = TypeCasting . cast_field ( sk , attributes [ range_key ] )
115
- sk_dumped = Dumping . dump_field ( sk_casted , attributes [ range_key ] )
116
-
117
- [ pk , sk_dumped ]
112
+ ids = ids . map do |id |
113
+ if range_key
114
+ # expect [hash key, range key] pair
115
+ pk , sk = id
116
+ pk_dumped = cast_and_dump ( hash_key , pk )
117
+ sk_dumped = cast_and_dump ( range_key , sk )
118
+
119
+ [ pk_dumped , sk_dumped ]
120
+ else
121
+ cast_and_dump ( hash_key , id )
118
122
end
119
123
end
120
124
@@ -155,15 +159,13 @@ def _find_all(ids, options = {})
155
159
def _find_by_id ( id , options = { } )
156
160
raise Errors ::MissingRangeKey if range_key && options [ :range_key ] . nil?
157
161
158
- if range_key
159
- key = options [ :range_key ]
160
- key_casted = TypeCasting . cast_field ( key , attributes [ range_key ] )
161
- key_dumped = Dumping . dump_field ( key_casted , attributes [ range_key ] )
162
+ partition_key_dumped = cast_and_dump ( hash_key , id )
162
163
163
- options [ :range_key ] = key_dumped
164
+ if range_key
165
+ options [ :range_key ] = cast_and_dump ( range_key , options [ :range_key ] )
164
166
end
165
167
166
- if item = Dynamoid . adapter . read ( table_name , id , options . slice ( :range_key , :consistent_read ) )
168
+ if item = Dynamoid . adapter . read ( table_name , partition_key_dumped , options . slice ( :range_key , :consistent_read ) )
167
169
model = from_database ( item )
168
170
model . run_callbacks :find
169
171
model
@@ -308,6 +310,14 @@ def method_missing(method, *args)
308
310
super
309
311
end
310
312
end
313
+
314
+ private
315
+
316
+ def cast_and_dump ( name , value )
317
+ attribute_options = attributes [ name ]
318
+ casted_value = TypeCasting . cast_field ( value , attribute_options )
319
+ Dumping . dump_field ( casted_value , attribute_options )
320
+ end
311
321
end
312
322
end
313
323
end
0 commit comments