I have the following class:
class CartItem < ApplicationRecord
belongs_to :inventory_item, polymorphic: true
end
Where an inventory item can be, say, a book or a pen.
Before, you could buy only pens, and the class looked like this:
class CartItem < ApplicationRecord
belongs_to :pen
end
As such, there are lots of references to @cart_item.pen in the codebase. Is there any automatic way in Rails to provide alias methods in the CartItem model to access a pen if a pen is stored in the inventory_item columns? I assumed I would have to write these #pen and #pen= methods by hand, but when I suggested I do that the team lead said:
so you shouldn't have to add either the setter or getter method if you set up the polymorphic relationship correctly
since a cart_item would still belong to a pen (polymorphically) both of those methods would exist for you
If he's right, what should I do?