In Ruby-on-Rails, this is called a "polymorphic association."
I have several Commentable
things in my application, the tables for each are below:
Post
id | title | text | author (FK:Person.id) | ...
Person
id | name | ...
Photo
id | title | owner (FK:Person.id) | path | ...
I'd like to add a Comments
table as follows:
Comments
id | commentable_type | commentable_id | text | author (FK:Person.id)
I understand that I lose the database's referential integrity this way, but the only other option is to have multiple Comments
tables: PostComments
, PersonComments
, PhotoComments
, ...
And now for the question:
How can I build a form that will grok how to do the lookup, first by getting the table name from Comments.commentable_type
and then the id from Comments.commentable_id
?