Skip to content

activerecord: Instance self type for the :default option argument in belongs_to #1045

Open
felixefelip wants to merge 1 commit into
ruby:mainfrom
felixefelip:activerecord/belongs_to_default_self_type
Open

activerecord: Instance self type for the :default option argument in belongs_to #1045
felixefelip wants to merge 1 commit into
ruby:mainfrom
felixefelip:activerecord/belongs_to_default_self_type

Conversation

@felixefelip

@felixefelip felixefelip commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

In Active Record belongs_to option :default, the self type in block is the instance type instead the singleton type. Doc: https://apidock.com/rails/ActiveRecord/Associations/ClassMethods/belongs_to#:~:text=belongs%5Fto%20%3Aaccount%2C%20default%3A%20%2D%3E%20%7B%20company%2Eaccount%20%7D

In applications like Fizzy, it's a very common use.

Without this fix, we have this output error:

activerecord-8.0.rb:37:36: [error] Type `singleton(::Test::Article)` does not have method `default_user`
│ Diagnostic ID: Ruby::NoMethod
│
└     belongs_to :user, default: -> { default_user }
                                      ~~~~~~~~~~~~

Detected 1 problem from 1 file

@felixefelip felixefelip changed the title Self type in default argument in Active Record belongs_to Instance self type in the :default option argument in Active Record belongs_to Jun 24, 2026
@felixefelip felixefelip changed the title Instance self type in the :default option argument in Active Record belongs_to Instance self type for the :default option argument in Active Record belongs_to Jun 24, 2026
extend ::ActiveRecord::Validations::ClassMethods
extend ::ActiveSupport::Callbacks::ClassMethods

type belongs_to_default[T] = ^(T) [self: T] -> ::ActiveRecord::Base?

@felixefelip felixefelip Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the Active Record 6, I couldn't find a test setup, so I didn't add a test to this change. But if it's necessary, I'd be happy to add tests for this version as well.

@felixefelip felixefelip marked this pull request as ready for review June 24, 2026 17:28
@github-actions

Copy link
Copy Markdown

@felixefelip Thanks for your contribution!

Please follow the instructions below for each change.
See also: https://github.com/ruby/gem_rbs_collection/blob/main/docs/CONTRIBUTING.md

Available commands

You can use the following commands by commenting on this PR.

  • /merge: Merge this PR if CI passes

activerecord

You changed RBS files for an existing gem.
You need to get approval from the reviewers of this gem.

@hibariya, @ksss, @Little-Rubyist, @tk0miya, please review this pull request.
If this change is acceptable, please make a review comment including APPROVE from here.
Screen Shot 2024-03-19 at 14 13 36

After that, the PR author or the reviewers can merge this PR.
Just comment /merge to merge this PR.

@felixefelip felixefelip force-pushed the activerecord/belongs_to_default_self_type branch from 4b9153c to 7c2914b Compare June 24, 2026 17:30
@felixefelip felixefelip changed the title Instance self type for the :default option argument in Active Record belongs_to activerecord: Instance self type for the :default option argument in belongs_to Jun 24, 2026
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.

1 participant