Skip to content

Commit 18ecbe9

Browse files
committed
Avoid casting to an integer
Not all ids are integers. Existing tests still pass without the cast, and removing the cast causes uuid joins to work.
1 parent c7e444e commit 18ecbe9

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

activerecord/lib/active_record/disable_joins_association_relation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def load
3030
record[key]
3131
end
3232

33-
records = ids.flat_map { |id| records_by_id[id.to_i] }
33+
records = ids.flat_map { |id| records_by_id[id] }
3434
records.compact!
3535

3636
@records = records

activerecord/test/cases/adapters/postgresql/uuid_test.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,63 @@ def test_find_by_with_uuid
413413
assert_nil UuidPost.find_by(id: 789)
414414
end
415415
end
416+
417+
class PostgresqlUUIDHasManyThroughDisableJoinsTest < ActiveRecord::PostgreSQLTestCase
418+
include PostgresqlUUIDHelper
419+
420+
class UuidForum < ActiveRecord::Base
421+
self.table_name = "pg_uuid_forums"
422+
has_many :uuid_posts, -> { order("title DESC") }
423+
has_many :uuid_comments, through: :uuid_posts
424+
has_many :uuid_comments_without_joins, through: :uuid_posts, source: :uuid_comments, disable_joins: true
425+
end
426+
427+
class UuidPost < ActiveRecord::Base
428+
self.table_name = "pg_uuid_posts"
429+
belongs_to :uuid_forum
430+
has_many :uuid_comments
431+
end
432+
433+
class UuidComment < ActiveRecord::Base
434+
self.table_name = "pg_uuid_comments"
435+
belongs_to :uuid_post
436+
has_one :uuid_forum, through: :uuid_post
437+
has_one :uuid_forum_without_joins, through: :uuid_post, source: :uuid_forum, disable_joins: true
438+
end
439+
440+
setup do
441+
connection.transaction do
442+
connection.create_table("pg_uuid_forums", id: :uuid, **uuid_default) do |t|
443+
t.string "name"
444+
end
445+
connection.create_table("pg_uuid_posts", id: :uuid, **uuid_default) do |t|
446+
t.references :uuid_forum, type: :uuid
447+
t.string "title"
448+
end
449+
connection.create_table("pg_uuid_comments", id: :uuid, **uuid_default) do |t|
450+
t.references :uuid_post, type: :uuid
451+
t.string "content"
452+
end
453+
end
454+
end
455+
456+
teardown do
457+
drop_table "pg_uuid_comments"
458+
drop_table "pg_uuid_posts"
459+
drop_table "pg_uuid_forums"
460+
end
461+
462+
def test_uuid_primary_key_and_disable_joins_with_delegate_cache
463+
uuid_forum = UuidForum.create!
464+
uuid_post_1 = uuid_forum.uuid_posts.create!
465+
uuid_comment_1_1 = uuid_post_1.uuid_comments.create!
466+
uuid_comment_1_2 = uuid_post_1.uuid_comments.create!
467+
uuid_post_2 = uuid_forum.uuid_posts.create!
468+
uuid_comment_2_1 = uuid_post_2.uuid_comments.create!
469+
uuid_comment_2_2 = uuid_post_2.uuid_comments.create!
470+
uuid_comment_2_3 = uuid_post_2.uuid_comments.create!
471+
472+
assert_equal uuid_forum.uuid_comments_without_joins.order(:id).to_a.map(&:id).sort,
473+
[uuid_comment_1_1.id, uuid_comment_1_2.id, uuid_comment_2_1.id, uuid_comment_2_2.id, uuid_comment_2_3.id].sort
474+
end
475+
end

0 commit comments

Comments
 (0)