All threads / Attaching a Characteristic To Multiple Models

Ask A Question


You’re not receiving notifications from this thread.

Attaching a Characteristic To Multiple Models

Chris Zempel asked in Rails

I've got these models:

class Firm
  has_many :funds

class Fund
  belongs_to :firm
  has_many :investments

class Investment
  belongs_to :fund
  has_many :investments

class Company
  belongs_to :investment

With each one of these models, I'm directly concerned with the identity and thread of continuity each one has over time, and not necessarily what information there actually is about it (I don't care what info exists about a company at a given time, just that the info for a company actually belongs to that company).

Now each of these models need to have a cashflow attached to them. This object doesn't really have a conceptual identity, it just describes a particular characteristic of the model it's attached to (info about money in, money out, and when it occurs). Right now, I see a couple ways forward:

1) Make a single cashflow class that I can attach agnostically to any aforementioned model. The gist of the implementation looking like this:

class Cashflow
  #has attached_id, attached_type
  def belongs_to

#Then I can also write some cashflow-related methods that I attach to the models through a concern or something like this: 

class Company
  def cashflows
    Cashflow.where(belongs_to: self)

2) Make individual cashflow classes for every model

class CompanyCashflow
  #does cashflow stuff

#FirmCashflow, FundCashflow, etc...

3) Or a more polymorphic approach: have a base cashflow class that I instantiate in a scoped way, so I can isolate model-specific logic (which I'm not sure if I'll need yet) there:

class Cashflow < ActiveRecord::Base
  #cashflow stuff

  def initialize(model)
    attached_type = model.class
    attached_id =

class CompanyCashflow < Cashflow
  #company specific stuff
  def initialize(model)
    #does company-specific stuff

@cashflow =

The main axis I want to think upon here is, eventual efficiency. If I have to go in and change this down the line after I have production data, that's not very efficient. First, how would you attach a cashflow to these other models? But second, and more importantly, what sort of reasoning, tradeoffs, and experiences inform your choice of designing the domain this way (or most pertinently, what's the very first thing you think before diving down into an intuitive reaction?)

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 37,629+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.