Controllers for Delegated Types

Hi all — I’m currently experimenting with the “Entry” pattern elucidated with the introduction of delegated_type in Rails 6.1. Seems like a very powerful pattern to build a project upon, which I’m excited about.

At the risk of asking naive questions to a bunch of experts… how should controllers look for the various entryable types’ controllers (e.g. Messages and Comments from the official examples)?

I’ve searched up all the public repos using delegated_type on GitHub, but it seems like most of them don’t have controllers for those resources.

In my tests, the create action in a scaffolded Messages controller won’t Create an associated Entry. I can see the server SELECTing on the Entry table for the appropriate entryable_id + entryable_type, but it doesn’t exist and it doesn’t attempt to create one.

Is that expected?

The suggestion from the docs is to use:

Entry.create! entryable: Comment.new(content: "Hello!"), creator: Current.user

Is that necessary for Controllers — and, if so, should that be included as a concern so that each Entryable type’s controller can just include the Entry creation behaviour?

Thanks for reading. Any advice welcome :slight_smile:

1 Like

@djch I have the same question as well - was wondering if you found some answers / best practices to handle this?

Hi @djch @BenKoshy same question here. Were you able to find some answers since last year? Thanks in advance

I hacked out some type of solution (don’t remember what i did). Best practices would be very handy for this, tested in the crucible of a production environment.