From 103f788f07d3410955348b972c5f229dd3412764 Mon Sep 17 00:00:00 2001 From: marc tobias Date: Fri, 12 Dec 2025 00:51:22 +0100 Subject: [PATCH] exists? to support ids which are strings --- lib/active_hash/base.rb | 2 +- spec/active_hash/base_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/active_hash/base.rb b/lib/active_hash/base.rb index 4bffad2..00d836b 100644 --- a/lib/active_hash/base.rb +++ b/lib/active_hash/base.rb @@ -113,7 +113,7 @@ def exists?(args = :none) elsif args.is_a?(Hash) all.where(args).present? else - all.where(id: args.to_i).present? + all.where(id: args.to_s).present? end end diff --git a/spec/active_hash/base_spec.rb b/spec/active_hash/base_spec.rb index c268815..c9420e5 100644 --- a/spec/active_hash/base_spec.rb +++ b/spec/active_hash/base_spec.rb @@ -1246,6 +1246,16 @@ def continent end end end + + context "when ids are strings" do + before do + Country.all.each { |c| c.id = c.name } + end + + it "return true" do + expect(Country.exists?("Canada")).to be_truthy + end + end end describe "#method_missing" do