Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Unsaved models can have nil non-nilable belongs-to relationships #253

Open
lrworth opened this issue Jan 10, 2020 · 1 comment
Open

Unsaved models can have nil non-nilable belongs-to relationships #253

lrworth opened this issue Jan 10, 2020 · 1 comment
Labels
bug Something isn't working

Comments

@lrworth
Copy link

lrworth commented Jan 10, 2020

Describe the bug:
The belongs_to relation is required by default (see #144) but objects with this relation don't necessarily have an object there. I believe the presence of the field is checked on save, not on construction. This leads to code which typechecks but fails with undefined method f for nil:NilClass at runtime.

Steps to reproduce:
I haven't had time to create a minimal reproduction, but:

  1. Create a non-optional belongs_to relation between two ActiveRecord models.
  2. Assert the generated type of this relation is not T.nilable.
  3. Construct a default instance of the model with the relation and call the method.

It will result in undefined method f for nil:NilClass.

Expected behavior:
The generated type should be T.nilable because there are no static constraints preventing this (at least for unsaved models).

Versions:

  • Ruby: ruby 2.6.4p104 (2019-08-28) [x86_64-darwin17]
  • Rails: Rails 5.2.4.1
  • Sorbet: Sorbet typechecker 0.4.5144 git dbd5bee6b64af3f39b4291f64eec555f2fbb008f built on 2019-12-17 00:46:19 GMT debug_symbols=true
  • Sorbet-Rails: sorbet-rails (0.5.9.1)
@lrworth lrworth added the bug Something isn't working label Jan 10, 2020
@hdoan741
Copy link
Contributor

hdoan741 commented Jan 11, 2020 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants