Skip to content

exists? to support ids which are strings#339

Merged
kbrock merged 1 commit intoactive-hash:masterfrom
mtmail:exists-string-ids
Mar 29, 2026
Merged

exists? to support ids which are strings#339
kbrock merged 1 commit intoactive-hash:masterfrom
mtmail:exists-string-ids

Conversation

@mtmail
Copy link
Copy Markdown
Contributor

@mtmail mtmail commented Dec 11, 2025

It's my understanding ActiveRecord supports ids which are strings (for example UUIDs).

Currently (version 4.0)

class Car < ActiveHash::Base
  self.data = [
    id: 'bmw', title: 'BMW'
  ]
end

Car.find('bmw').is_a?(Car)
 => true
Car.find_by(id: 'bmw').is_a?(Car)
 => true
Car.where(id: 'bmw').first.is_a?(Car)

Car.exists?('bmw')
 => false
Car.exists?(id: 'bmw')
 => true

I would've expected Car.exists?('bmw') to return true.

In active_hash/base.rb I see ids are converted to string in several places for comparison, and the record_index also stores the keys as string.

Copy link
Copy Markdown
Collaborator

@kbrock kbrock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are tests that verify this still works with find(int) and find(str)

[base_spec.rb:733]https://github.com/mtmail/active_hash/blob/103f788f07d3410955348b972c5f229dd3412764/spec/active_hash/base_spec.rb#L733-L740

and exists?(int)

base_spec.rb:1205

@kbrock kbrock merged commit e93762d into active-hash:master Mar 29, 2026
20 checks passed
@stefafafan stefafafan mentioned this pull request Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants