Polymorphic association, can I have one association not backed by a table?

Hi, I’ve got an events table storing intervals of events with a from, to, eventable_type and eventable_id. This is working really well for all cases bar one. All these cases work because an event is always tied back to a record, such as on the timeline let’s say someone started an event like a Part on a machine, then stopped it. Then the Part event is tied to a specific part

I have one case in particular though where there’s no real concrete specific record that was started and stopped, this is more like an event that has no extra data associated with it, such as a PartCount. The partcount is an instant (same start and end), and wants to be plotted on the timeline, but it doesn’t refer to anything specific. It’s just a dumb thing with a type, start and end, and machine_id

As far as I can tell polymorphism doesn’t allow a nil eventable_id.

So I could create for example a part_counts table that just has a master day. that all events tie back to, but this seems redundant. Could I do this in code instead? Faking a part_counts table, with one that’s got an id of 1? Rather than running migrations on the database.

Or is there a way a polymorphic association can refer to a PORO, just with the eventable_type, and doesn’t event need an eventable_id.

relevant code:

class Event < ApplicationRecord
  belongs_to :eventable, polymorphic: true, optional: true

  belongs_to :machine
  belongs_to :user, optional: true

end

What I’d like is something like this

class PartCount < ApplicationRecord
   self.abstract_class = true
end

You can create a tableless base model like so:

class Unpersisted::Base < ActiveRecord::Base
  self.abstract_class = true

  def self.attribute_names
    @attribute_names ||= attribute_types.keys
  end

  def self.load_schema!
    @columns_hash ||= {}.freeze
    return
  end

  def persisted?
    return false
  end
end

You can make your “PORO” class inherit from it and define an association. You may have to tweak some validations and/or setters, especially if you need eager loading.

Great I’ll try that thank you. I need to eager load it yeah