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.
This commit is contained in:
Samuel Cochran 2023-02-23 16:42:42 +11:00
parent c7e444e3b0
commit 18ecbe987e
No known key found for this signature in database
GPG Key ID: 1A36ACA1BDECD23E
2 changed files with 61 additions and 1 deletions

@ -30,7 +30,7 @@ def load
record[key]
end
records = ids.flat_map { |id| records_by_id[id.to_i] }
records = ids.flat_map { |id| records_by_id[id] }
records.compact!
@records = records

@ -413,3 +413,63 @@ def test_find_by_with_uuid
assert_nil UuidPost.find_by(id: 789)
end
end
class PostgresqlUUIDHasManyThroughDisableJoinsTest < ActiveRecord::PostgreSQLTestCase
include PostgresqlUUIDHelper
class UuidForum < ActiveRecord::Base
self.table_name = "pg_uuid_forums"
has_many :uuid_posts, -> { order("title DESC") }
has_many :uuid_comments, through: :uuid_posts
has_many :uuid_comments_without_joins, through: :uuid_posts, source: :uuid_comments, disable_joins: true
end
class UuidPost < ActiveRecord::Base
self.table_name = "pg_uuid_posts"
belongs_to :uuid_forum
has_many :uuid_comments
end
class UuidComment < ActiveRecord::Base
self.table_name = "pg_uuid_comments"
belongs_to :uuid_post
has_one :uuid_forum, through: :uuid_post
has_one :uuid_forum_without_joins, through: :uuid_post, source: :uuid_forum, disable_joins: true
end
setup do
connection.transaction do
connection.create_table("pg_uuid_forums", id: :uuid, **uuid_default) do |t|
t.string "name"
end
connection.create_table("pg_uuid_posts", id: :uuid, **uuid_default) do |t|
t.references :uuid_forum, type: :uuid
t.string "title"
end
connection.create_table("pg_uuid_comments", id: :uuid, **uuid_default) do |t|
t.references :uuid_post, type: :uuid
t.string "content"
end
end
end
teardown do
drop_table "pg_uuid_comments"
drop_table "pg_uuid_posts"
drop_table "pg_uuid_forums"
end
def test_uuid_primary_key_and_disable_joins_with_delegate_cache
uuid_forum = UuidForum.create!
uuid_post_1 = uuid_forum.uuid_posts.create!
uuid_comment_1_1 = uuid_post_1.uuid_comments.create!
uuid_comment_1_2 = uuid_post_1.uuid_comments.create!
uuid_post_2 = uuid_forum.uuid_posts.create!
uuid_comment_2_1 = uuid_post_2.uuid_comments.create!
uuid_comment_2_2 = uuid_post_2.uuid_comments.create!
uuid_comment_2_3 = uuid_post_2.uuid_comments.create!
assert_equal uuid_forum.uuid_comments_without_joins.order(:id).to_a.map(&:id).sort,
[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
end
end