@@ -109,12 +109,16 @@ def find_by_id(id, options = {})
109109 def _find_all ( ids , options = { } )
110110 raise Errors ::MissingRangeKey if range_key && ids . any? { |_pk , sk | sk . nil? }
111111
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 )
118122 end
119123 end
120124
@@ -155,15 +159,13 @@ def _find_all(ids, options = {})
155159 def _find_by_id ( id , options = { } )
156160 raise Errors ::MissingRangeKey if range_key && options [ :range_key ] . nil?
157161
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 )
162163
163- options [ :range_key ] = key_dumped
164+ if range_key
165+ options [ :range_key ] = cast_and_dump ( range_key , options [ :range_key ] )
164166 end
165167
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 ) )
167169 model = from_database ( item )
168170 model . run_callbacks :find
169171 model
@@ -308,6 +310,14 @@ def method_missing(method, *args)
308310 super
309311 end
310312 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
311321 end
312322 end
313323end
0 commit comments