Reputation: 63
I'm building a fairly complex web application in Rails and have come across an issue that I suspect some have encountered before but for which I cannot find a generally agreed upon solution. The issue concerns referencing resources of arbitrary types (and with arbitrary corresponding tables) from a single model property/table column. My use case is a simple news feed, where feed items have the form:
id (int)
tag (string)
message (string)
expiry (date)
resource_id (string)
"resource_id" is a reference to the resource that is centrally concerned by the feed item. In my project, I presently have it taking the form "class_name:id". When fetching news feed items, I construct a set of strings of this form corresponding to resources owned by the user and run an "in" type query on the resource_id column. It's relevant to note that the structure of the app is such that users will only own a small (< 10) number of these resources so the efficiency of lookup isn't a problem with this structure. It just feels like a hacky and very wrong solution and I suspect there's a more correct way to do what I'm trying to do.
Upvotes: 0
Views: 96
Reputation: 26979
I'd use two columns: resource_type and resource_id. Makes the joins more normalized.
Upvotes: 1