Skip to content

Add exists? to Relation#349

Open
michebble wants to merge 2 commits into
active-hash:masterfrom
michebble:add-exists-to-relation
Open

Add exists? to Relation#349
michebble wants to merge 2 commits into
active-hash:masterfrom
michebble:add-exists-to-relation

Conversation

@michebble

@michebble michebble commented Jun 26, 2026

Copy link
Copy Markdown

This PR adds exists? to Relation so that users can call the method after applying a scope to an ActiveHash model.

This brings the behaviour inline with ActiveRecord.

Problem

Calling exists? after apply scope/s results in NoMethodError being raised.

class Country < ActiveHash::Base
  scope :north_america, -> { where(continent: 'North America') }
  scope :south_america, -> { where(continent: 'South America') }

  self.data = [
    { id: 1, name: 'US', continent: 'North America' },
    { id: 2, name: 'Brazil', continent: 'South America' }
  ]
end

Without PR changes

Country.exists? 1
# true
Country.north_america.exists? 1
# undefined method 'exists?' for an instance of ActiveHash::Relation (NoMethodError)

With PR changes

Country.exists? 1
# true
Country.north_america.exists? 1
# true

initial comment

ActiveRecord allows calling exists? on a relation, however ActiveHash currently only allows calling it on the base model. Adding the method to Relation provides functionality that those familiar with ActiveRecord expect.

I needed to monkey patch my app to get this behaviour and would appropriate it if you would consider including this in the next release. I would like to get rid of the monkey patch if possible

ActiveRecord allows calling exists? on Realtions, however ActiveHash
currently only allows calling it on the base model. Adding the method to
Realtion provides functionality that those familar with ActiveRecord
expect.
@flavorjones

Copy link
Copy Markdown
Collaborator

@michebble We'd welcome a pull request for this, if you're up for it!

@michebble

michebble commented Jul 13, 2026

Copy link
Copy Markdown
Author

Thank you @flavorjones . I have updated the comment to make it clear what this PR changes.
Please let me know if there is anything else I can do to get the PR in good shape.

@kbrock kbrock left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This looks great.

curious, did you try delegating exist? to :records?

.e.g.:

module ActiveHash
  class Relation
    # about line 8
    delegate :sample, :exists?, to: :records
  end
end

or updating one of the entries there.

try it out, and then ping if you have any issues.
This code is ok, but if you notice, we tried to avoid duplicating the method definitions

We can use the Relation implementation of exists? and then delegate the
method on Base. This will give us one implementation that can be used on
but the record class or a scoped relation.
@michebble

Copy link
Copy Markdown
Author

I was hoping it would be that simple at first, but records is just an Array. After stewing on it a little longer, I realised that Base delegates a lot of methods to Relation (as all) so we could do the same with exists?

I believe this matches the spirit of your suggestion and the specs are still green. Thank you for the comment.

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.

3 participants