Skip to content

Add ActiveHash::Relation#or to support OR-like queries#343

Merged
kbrock merged 1 commit intoactive-hash:masterfrom
opsys-saito:feature/add-or-support
Feb 24, 2026
Merged

Add ActiveHash::Relation#or to support OR-like queries#343
kbrock merged 1 commit intoactive-hash:masterfrom
opsys-saito:feature/add-or-support

Conversation

@opsys-saito
Copy link
Contributor

Summary

This PR adds support for ActiveHash::Relation#or, allowing OR-like queries similar to ActiveRecord:

Model.where(condition_a).or(Model.where(condition_b))

The implementation returns a new relation representing the union of both result sets, deduplicated by id.

Behavior

  • Supports chaining:
    Model.where(a: 1).or(Model.where(a: 2)).where(b: 3)
  • Deduplicates records based on id
  • Raises an ArgumentError when combining relations from different models

Implementation Notes

Since ActiveHash operates entirely in memory, this implementation performs a union of the evaluated result sets rather than merging condition trees.

The returned value remains an ActiveHash::Relation, preserving chainability and compatibility with existing APIs.

Tests

Added specs covering:

  • Union of two relations
  • Deduplication behavior
  • Chainability after or
  • Error handling for mismatched models

@opsys-saito opsys-saito changed the title Add ActiveHash::Relation#or to support OR-like queriesAdd Relation#or support Add ActiveHash::Relation#or to support OR-like queries Feb 19, 2026
Copy link
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.

This looks sweet.

Could you add a or() with anoother operator, like an order?

@opsys-saito
Copy link
Contributor Author

Thanks! That's a great point 👍

Just to clarify — would it make sense to support or for relations with additional operators like order only when those operators are identical?

In that case, we could raise an error when they differ, similar to ActiveRecord’s behavior for incompatible relations.

Happy to adjust depending on your preference!

@kbrock
Copy link
Collaborator

kbrock commented Feb 22, 2026

@opsys-saito sorry. I was not clear

Please just add a test with an order.

Something like?

Hoping Model.where(:id => 1).or(Model.where(:id => [2, 3])).order(:id => :desc)

Maybe using country - just throw an order in there ensure it works, and call it a day.

@opsys-saito opsys-saito force-pushed the feature/add-or-support branch from 6b84886 to 07dbfdb Compare February 23, 2026 03:27
@opsys-saito
Copy link
Contributor Author

@kbrock

Thanks for the clarification!

I've added a test to ensure or works correctly when combined with order. Let me know if anything else should be covered.

Copy link
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.

This looks great. thanks

Just waiting for Green then I'll merge

@kbrock kbrock merged commit 192dfb1 into active-hash:master Feb 24, 2026
20 checks passed
@opsys-saito
Copy link
Contributor Author

@kbrock
Thanks for merging this! Really looking forward to the release 🚀

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