Module: Label::Relation::BidirectionalRelationExtension
- Defined in:
- app/models/label/relation/bidirectional_relation_extension.rb
Instance Method Summary collapse
- #create_with_bidirectional_relation(target_label, attributes = {}) ⇒ Object
- #destroy_with_bidirectional_relation(target_label) ⇒ Object
Instance Method Details
#create_with_bidirectional_relation(target_label, attributes = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/models/label/relation/bidirectional_relation_extension.rb', line 20 def create_with_bidirectional_relation(target_label, attributes = {}) relation_class = proxy_association.reflection.class_name.constantize ActiveRecord::Base.transaction do # The one direction scope = relation_class.where(domain_id: proxy_association.owner.id, range_id: target_label.id) scope.any? || scope.create!(attributes) # The bidirectional direction scope = relation_class.where(domain_id: target_label.id, range_id: proxy_association.owner.id) scope.any? || scope.create!(attributes) end end |
#destroy_with_bidirectional_relation(target_label) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/label/relation/bidirectional_relation_extension.rb', line 33 def destroy_with_bidirectional_relation(target_label) relation_class = proxy_association.reflection.class_name.constantize ActiveRecord::Base.transaction do relation_class.where(domain_id: proxy_association.owner.id, range_id: target_label.id).load.each do |relation| relation.destroy end relation_class.where(domain_id: target_label.id, range_id: proxy_association.owner.id).load.each do |relation| relation.destroy end end end |