I have a polymorphic model Comments that I associate with almost every other model. I have comments index page where I use the title of the model, which I call commentable.
What is the best way to ensure that the commentable model has a method or attribute called :title?
In Objective-C I would define a protocol and in other languages I would define an abstract class or interface. I’m trying to figure out the best way of writing maintainable rails code.
I would write a method on the comments model that does this for you. So in comments.rb:
In your view, you can check if the comment has a title and render it, or else just skip rendering:
It’s not exactly a protocol or interface, but in Ruby you can’t easily mandate that objects act in a certain way. You use things like respond_to? to see if they do, and if so, then you act.