Inheritance and Persistence
Aside from utility functions, most polymorphism in web programming is related to inheritance, be it strict class inheritance or implementation of interfaces through module includes.
The consideration, then, is how to store these hierarchies of objects in the database. Should the data be stored in a single table or in multiple tables? And if the latter, how do we do this in the context of Rails using ActiveRecord? In this chapter, we’ll expand upon the payment data model we left off with in Chapter 5, reworking it to be more complete from the perspective of our real-world application. In doing so, we’ll encounter a case of STI, which Rails supports by default. We’ll also encounter an example of MTI.
Since our application is about to get rather complex, we begin by
developing a logical model for the order payment system before we jump
to the physical model.[3] The logical model is shown in Figure 10-2. We still have an
order, which contains multiple tickets. However, we now split off the
payment into its own class. We have three distinct types of payments: a
credit card payment, as we had previously, a Paypal payment, and also a
free promotional payment, which might be granted for entering a valid
promotional code. The CreditCard
class is a super class of three accepted card types: AmEx, Visa, and
MasterCard. All credit card payments need address information, so the
CreditCard
class also implements the
Address
interface via a module
include.
Figure 10-2. Logical ...
Get Enterprise Rails now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.