Module: Tenacity::Associations::HasMany
- Included in:
- Tenacity
- Defined in:
- lib/tenacity/associations/has_many.rb
Overview
:nodoc:
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #_t_cleanup_has_many_association(association) ⇒ Object
- #_t_get_associate_ids(association) ⇒ Object
- #_t_remove_associates(association) ⇒ Object
Instance Method Details
#_t_cleanup_has_many_association(association) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/tenacity/associations/has_many.rb', line 9 def _t_cleanup_has_many_association(association) associates = has_many_associates(association) unless associates.nil? || associates.empty? if association.dependent == :destroy delete_or_destroy_has_many_associates(association, associates) elsif association.dependent == :delete_all delete_or_destroy_has_many_associates(association, associates, false) elsif association.dependent == :nullify nullify_foreign_keys_for_has_many_associates(association, associates) elsif association.foreign_key_constraints_enabled? raise ObjectIdInUseError.new("Unable to delete #{self.class} with id of #{self.id} because its id is being referenced by instances of #{associates.first.class}(id: #{associates.map(&:id).join(',')})!") end end end |
#_t_get_associate_ids(association) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/tenacity/associations/has_many.rb', line 24 def _t_get_associate_ids(association) if self.id.nil? [] else foreign_key = association.foreign_key(self.class) associate_id = self.class._t_serialize_ids(self.id, association) ids = association.associate_class._t_find_all_ids_by_associate(foreign_key, associate_id) self.class._t_serialize_ids(ids, association) end end |
#_t_remove_associates(association) ⇒ Object
5 6 7 |
# File 'lib/tenacity/associations/has_many.rb', line 5 def _t_remove_associates(association) instance_variable_set(_t_ivar_name(association), []) end |