I have a situation that I am struggling with conceptually. I have a class Media, which represents (unsurprisingly) a peice of media. Now, there are many several different classes that own Media – for example, a Question can own a Media object, an Answer can also own a Media object.
Trying to model this from the direction of the owning class is easy, I just give them the appropriate has_one or has_many associations. However, I am struggling in the other direction. A media does not belong_to any particular class, it can belong to any of a set of classes.
I suspect that the appropriate thing to do in these moments of ambiguity, is to simply NOT annotate the class with any belongs_to association. However, I can’t find any documentation confirming this.
What you’re looking for is a polymorphic relationship.
I answered a similar question where the same media could belong to more than one model. You may find that answer useful. https://stackoverflow.com/a/8237597/367611