Skip to content

Instantly share code, notes, and snippets.

@ornerymoose
Created November 16, 2017 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ornerymoose/543307b7200dc7adcda46e34b37b54a4 to your computer and use it in GitHub Desktop.
Save ornerymoose/543307b7200dc7adcda46e34b37b54a4 to your computer and use it in GitHub Desktop.
class Outage < ApplicationRecord
has_many :relationships
has_many :children, through: :relationships
end
class Child < ApplicationRecord
has_many :relationships
has_many :outages, through: :relationships
end
class Relationship < ApplicationRecord
belongs_to :child
belongs_to :outage
validate :ensure_one_relationship
def ensure_one_relationship
if Relationship.where(child_id: self.child_id).present?
errors.add(:base, "A trouble ticket can only be associated with one outage ticket.")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment