Fake Multiple Table Inheritance
For many years now a pattern has emerged in my Rails websites. Most models will have Title, Description, Online at, Offline at, Is visible, Url and Image_file_name fields. It screams to be solved using an underlying common model using the “is_a” pattern. There is no clean way to solve this in Rails. I discovered this post though, which outlines a good method to solve this problem.
For the sake of Google I am going to spend the rest of this post using various phrases in the hopes of tying this post to various (desperate) searches, as I have done so many times in the past. Multiple Table Inheritance with Active Record describes the pattern in which you have a has_one relationship between your models and a common subset of attributes. Your model will always have the other “has_one” shared model, they are tied together. Its attributes become part of the model itself. It solves the problem of a Content Management System in which you always have a title, description and publication dates for lots of different objects.
No comments yet.