Last active
November 24, 2017 16:48
-
-
Save ornerymoose/863d6b0ccd1e036792d9af229b6be7c8 to your computer and use it in GitHub Desktop.
Given the custom validation in relationship.rb, if I try to update an outage with a child that is already associated to a different outage, it gives me a validation error. It's trying to create a new relationship, not update one. If I pass the on: :create to the custom validation, that won't work since it's trying to create a new relationship on…
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 Child < ApplicationRecord | |
has_many :relationships | |
has_many :outages, through: :relationships | |
end |
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 | |
def self.destroy_current_relationship(outage_id, child_id) | |
Relationship.where(outage_id: outage_id).where(child_id: child_id).destroy_all | |
end | |
end |
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 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 |
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
<%= form_for(@outage) do |f| %> | |
<p> | |
<div class="form-group"> | |
<%= collection_check_boxes(:outage, :child_ids, Child.all, :id, :omnia_trouble_ticket) do |b| %> | |
<div class="col-xs-12"> | |
<div class="row"> | |
<%= b.label(class: "check_box") do %> | |
<%= b.check_box(class: "check_box") %><%= b.object.omnia_trouble_ticket %> | |
<% end %> | |
</div> | |
</div> | |
<% end %> | |
</div> | |
</p> | |
<%= f.submit("Update Outage", class: "btn btn-default") %> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment