Alias scopes for delegated types with `for_`

I think we should add an alias method for scopes by type for delegated_type like so

`Entry.for_comments` # instead of Entry.comments

Why?

This feels more semantically correct, as Entry.comments implies the method to return comments, when in fact it returns entries. An example

# model
class Recommendation
  delegated_type :recommendable, types: %w[Wine Shop]
end

# controller action

@entries = current_user.recommendations.wines
# in isolation the methods implies I get wines, it does not look like a scope
# but rather like an association.

@entries = current_user.recommendations.for_wines
# makes immediately clear that no wines are returned

I created a PR to that effect: Alias scopes for delegated types with `for_` by RolandStuder · Pull Request #41506 · rails/rails · GitHub

1 Like