Wednesday, 11 September 2013

How to make a shared table flexible enough to service two models

How to make a shared table flexible enough to service two models

I'm working on an app that will help me track me finances. There's a model
that tracks my investment accounts (e.g. 401k, etc.) and the monthly
balance associated with each account. The investment_accounts model
has_many balances and the balances belong_to investment_accounts.
I'd like the app to now track debt, specifically my mortgage. But I'm
struggling to figure out how to best model this? I'll need to have a
mortgage model that has different information that than the
investment_accounts (specifically interest rate). But the mortgage model
should leverage the balances table, since tracking mortgage balances is so
similar to tracking investment account balances. But I'm at a loss for how
to revise the balances table to make it sufficiently flexible to service
both the mortgage table and the investment_account table.
I considered adding a 'type' attribute on the balances model and creating
named scopes like this:
scope :investment_accounts, where(type: 'investment')
scope :mortgage, where(type: 'mortgate')
But then I realized this should lead to some weird looking ActiveRecord
queries. For instances:
investment_accounts.balances.investment_accounts
and
mortgage.balances.mortgage
I take this as a code smell. Any suggestions on how to do this right?

No comments:

Post a Comment