class State < ApplicationRecord belongs_to :character belongs_to :condition after_create :overwrite_old_states def expired? self.expires_at < Time.now end def remaining_duration (self.expires_at - Time.now).to_i end def effects self.condition.whatnot[:effects] end private def overwrite_old_states # Don't allow duplicate conditions; just destroy the old one. character.states.where(condition: condition).where.not(id: id).destroy_all # Only allow one food condition and one drink condition. Destroy any old ones. %w[food drink].each do |tag| if condition.tags.include?(tag) character.states.each do |state| state.destroy if state.id != id && state.condition.tags.include?(tag) end end end end end