Declarative model broadcasts with broadcasts_to (Rails 7)

5581
0

When I’m on Rails 7+, I like broadcasts_to because it makes realtime behavior obvious in the model. Instead of writing explicit after_create_commit hooks, I declare that a model broadcasts to its parent or to a scope. Then Turbo uses conventional partial names to render updates. This is great for simple CRUD lists. The key is to keep the partials stable (_message.html.erb) and to be careful about where you broadcast (scope to a room, project, or account). I also like to use broadcasts_refreshes when the UI is complex and a full morph refresh is simpler than managing many targets. But for lists, broadcasts_to is clean and predictable.