diff --git a/lib/hashid/rails.rb b/lib/hashid/rails.rb index c0e4c81..de42016 100644 --- a/lib/hashid/rails.rb +++ b/lib/hashid/rails.rb @@ -55,7 +55,8 @@ def encode_id(ids) def decode_id(ids) if ids.is_a?(Array) - ids.map { |id| hashid_decode(id) } + decoded_ids = ids.map { |id| hashid_decode(id) } + decoded_ids.any? ? decoded_ids : nil else hashid_decode(ids) end diff --git a/spec/hashid/rails_spec.rb b/spec/hashid/rails_spec.rb index 43ba703..bdfba9f 100644 --- a/spec/hashid/rails_spec.rb +++ b/spec/hashid/rails_spec.rb @@ -31,6 +31,15 @@ expect(decoded_ids).to eql [1, 3, 5] end + it "finds muliple un-encoded ids" do + model_one = FakeModel.create! + model_two = FakeModel.create! + + FakeModel.find([model_one.id, model_two.id]) + + FakeModel.delete_all + end + it "encodes multiple ids" do encoded_ids = FakeModel.encode_id([2, 4, 6]) expect(encoded_ids).to eq %w(YznovR OeVre9 YNAOva)