Created
November 16, 2017 14:28
-
-
Save ornerymoose/543307b7200dc7adcda46e34b37b54a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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