Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation about using PrivateRelation #1266

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions manual/compiler_activerecordrelations.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,16 @@ class Post
end
end
~~~

**A note about migrating from `sorbet-rails`**: there is no direct analogue to `Post::RelationType` in Tapioca.

`sorbet-rails` actually [changes the way a Rails application loads](https://github.com/chanzuckerberg/sorbet-rails/blob/447ecbe4a2586ce8aa9055290d571fe34a55e27f/lib/sorbet-rails/railtie.rb#L26-L50) in order to make `RelationType` work at runtime. One strategy to add types for relations is to use `WithoutRuntime`. For example:

~~~rbi
T::Sig::WithoutRuntime.sig { returns(Post::PrivateRelation) }
def posts
Post.where(id: 123)
end
~~~

As the name suggests, this will give you type checking during development but disable it at runtime.
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be beneficial to give a bit more information here about how Tapioca defines methods on PrivateRelation that returns a Post type. For example, the first! method. That's why this approach gives more static typing benefits.

But I didn't use sorbet-rails and if you think this information is already assumed by an existing sorbet-rails user than it's okay to omit it.