Skip to content

Commit e4a7229

Browse files
committed
Monkey patch active record relation query attribute hash method to better compare objects
This a important since arel and AR commonly use array - operator
1 parent 44e7775 commit e4a7229

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
require "active_model/attribute"
4+
5+
module ActiveRecord
6+
# NOTE: improved implementation for hash methods that is used to
7+
# compare objects. AR and arel commonly use `[a, b] - [b]` operations and
8+
# JRuby internally uses the hash method to implement that operation,
9+
# on the other hand, CRuby does not use the hash method
10+
# for small arrays (length <= 16).
11+
class Relation
12+
# monkey patch
13+
module RelationQueryAttributeMonkeyPatch
14+
def hash
15+
# [self.class, name, value_for_database, type].hash
16+
[self.class, name, value_before_type_cast, type].hash
17+
end
18+
end
19+
20+
class QueryAttribute
21+
prepend RelationQueryAttributeMonkeyPatch
22+
end
23+
end
24+
end

lib/arjdbc/mysql/adapter.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
require 'arjdbc/abstract/statement_cache'
1212
require 'arjdbc/abstract/transaction_support'
1313

14+
require "arjdbc/abstract/relation_query_attribute_monkey_patch"
15+
1416
module ActiveRecord
1517
module ConnectionAdapters
1618
AbstractMysqlAdapter.class_eval do

lib/arjdbc/postgresql/adapter.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
require 'active_model'
2929

30+
require "arjdbc/abstract/relation_query_attribute_monkey_patch"
31+
3032
module ArJdbc
3133
# Strives to provide Rails built-in PostgreSQL adapter (API) compatibility.
3234
module PostgreSQL

lib/arjdbc/sqlite3/adapter.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
require "active_support/core_ext/class/attribute"
1919
require "arjdbc/sqlite3/column"
2020

21+
require "arjdbc/abstract/relation_query_attribute_monkey_patch"
22+
2123
module SQLite3
2224
module Constants
2325
module Open

0 commit comments

Comments
 (0)