From 7c2914b55c7f3686cfb2ab46e1cefa4dda0af7fb Mon Sep 17 00:00:00 2001 From: Felipe Felix Date: Wed, 24 Jun 2026 14:30:34 -0300 Subject: [PATCH] Fix instance self type in default option argument for the belongs_to Active Record --- gems/activerecord/6.0/activerecord.rbs | 4 +++- gems/activerecord/7.2/_test/activerecord-7.2.rb | 5 +++++ gems/activerecord/7.2/_test/activerecord-7.2.rbs | 2 ++ gems/activerecord/7.2/activerecord.rbs | 4 +++- gems/activerecord/8.0/_test/activerecord-8.0.rb | 5 +++++ gems/activerecord/8.0/_test/activerecord-8.0.rbs | 2 ++ gems/activerecord/8.0/activerecord.rbs | 4 +++- 7 files changed, 23 insertions(+), 3 deletions(-) diff --git a/gems/activerecord/6.0/activerecord.rbs b/gems/activerecord/6.0/activerecord.rbs index d79d80fa..92d2b955 100644 --- a/gems/activerecord/6.0/activerecord.rbs +++ b/gems/activerecord/6.0/activerecord.rbs @@ -42,9 +42,11 @@ module ActiveRecord extend ::ActiveRecord::Validations::ClassMethods extend ::ActiveSupport::Callbacks::ClassMethods + type belongs_to_default[T] = ^(T) [self: T] -> ::ActiveRecord::Base? + def self.abstract_class=: (bool) -> void def self.scope: (Symbol, Proc) ?{ () -> untyped } -> void - def self.belongs_to: (Symbol, ?untyped, **untyped) -> void + def self.belongs_to: (Symbol, ?untyped, **untyped, ?default: belongs_to_default[instance]) -> void def self.has_many: (Symbol, ?untyped, **untyped) -> void def self.has_one: (Symbol, ?untyped, **untyped) -> void def self.has_and_belongs_to_many: (untyped name, ?untyped? scope, **untyped options) ?{ () -> untyped } -> untyped diff --git a/gems/activerecord/7.2/_test/activerecord-7.2.rb b/gems/activerecord/7.2/_test/activerecord-7.2.rb index 3c87076a..040b879a 100644 --- a/gems/activerecord/7.2/_test/activerecord-7.2.rb +++ b/gems/activerecord/7.2/_test/activerecord-7.2.rb @@ -34,6 +34,11 @@ class User < ApplicationRecord end class Article < ApplicationRecord + belongs_to :user, default: -> { default_user } + + def default_user + User.first + end end User.where.missing.to_sql diff --git a/gems/activerecord/7.2/_test/activerecord-7.2.rbs b/gems/activerecord/7.2/_test/activerecord-7.2.rbs index 6319571f..65f304f2 100644 --- a/gems/activerecord/7.2/_test/activerecord-7.2.rbs +++ b/gems/activerecord/7.2/_test/activerecord-7.2.rbs @@ -21,5 +21,7 @@ module Test class ActiveRecord_Associations_CollectionProxy < ::ActiveRecord::Associations::CollectionProxy include ::ActiveRecord::Relation::Methods[Article, Integer] end + + def default_user: () -> User? end end diff --git a/gems/activerecord/7.2/activerecord.rbs b/gems/activerecord/7.2/activerecord.rbs index f26648ce..a0da28a2 100644 --- a/gems/activerecord/7.2/activerecord.rbs +++ b/gems/activerecord/7.2/activerecord.rbs @@ -42,9 +42,11 @@ module ActiveRecord extend ::ActiveRecord::Validations::ClassMethods extend ::ActiveSupport::Callbacks::ClassMethods + type belongs_to_default[T] = ^(T) [self: T] -> ::ActiveRecord::Base? + def self.abstract_class=: (bool) -> void def self.scope: (Symbol, Proc) ?{ () -> untyped } -> void - def self.belongs_to: (Symbol, ?untyped, **untyped) -> void + def self.belongs_to: (Symbol, ?untyped, **untyped, ?default: belongs_to_default[instance]) -> void def self.has_many: (Symbol, ?untyped, **untyped) -> void def self.has_one: (Symbol, ?untyped, **untyped) -> void def self.has_and_belongs_to_many: (untyped name, ?untyped? scope, **untyped options) ?{ () -> untyped } -> untyped diff --git a/gems/activerecord/8.0/_test/activerecord-8.0.rb b/gems/activerecord/8.0/_test/activerecord-8.0.rb index 3c87076a..040b879a 100644 --- a/gems/activerecord/8.0/_test/activerecord-8.0.rb +++ b/gems/activerecord/8.0/_test/activerecord-8.0.rb @@ -34,6 +34,11 @@ class User < ApplicationRecord end class Article < ApplicationRecord + belongs_to :user, default: -> { default_user } + + def default_user + User.first + end end User.where.missing.to_sql diff --git a/gems/activerecord/8.0/_test/activerecord-8.0.rbs b/gems/activerecord/8.0/_test/activerecord-8.0.rbs index 6319571f..65f304f2 100644 --- a/gems/activerecord/8.0/_test/activerecord-8.0.rbs +++ b/gems/activerecord/8.0/_test/activerecord-8.0.rbs @@ -21,5 +21,7 @@ module Test class ActiveRecord_Associations_CollectionProxy < ::ActiveRecord::Associations::CollectionProxy include ::ActiveRecord::Relation::Methods[Article, Integer] end + + def default_user: () -> User? end end diff --git a/gems/activerecord/8.0/activerecord.rbs b/gems/activerecord/8.0/activerecord.rbs index 9e560da6..5c4f4131 100644 --- a/gems/activerecord/8.0/activerecord.rbs +++ b/gems/activerecord/8.0/activerecord.rbs @@ -42,9 +42,11 @@ module ActiveRecord extend ::ActiveRecord::Validations::ClassMethods extend ::ActiveSupport::Callbacks::ClassMethods + type belongs_to_default[T] = ^(T) [self: T] -> ::ActiveRecord::Base? + def self.abstract_class=: (bool) -> void def self.scope: (Symbol, Proc) ?{ () -> untyped } -> void - def self.belongs_to: (Symbol, ?untyped, **untyped) -> void + def self.belongs_to: (Symbol, ?untyped, **untyped, ?default: belongs_to_default[instance]) -> void def self.has_many: (Symbol, ?untyped, **untyped) -> void def self.has_one: (Symbol, ?untyped, **untyped) -> void def self.has_and_belongs_to_many: (untyped name, ?untyped? scope, **untyped options) ?{ () -> untyped } -> untyped