diff options
author | brainopia <brainopia@evilmartians.com> | 2014-12-31 00:19:09 +0300 |
---|---|---|
committer | brainopia <brainopia@evilmartians.com> | 2014-12-31 00:19:09 +0300 |
commit | 7f3b246b99afc8bc6e591c8808a38556c400ad3c (patch) | |
tree | 9e567042b7d4b504ed63046e331a0f4a340b10d1 /activerecord/lib/active_record/associations | |
parent | 322750e213ee929752a8bb07dbe44b839452e0a3 (diff) | |
download | rails-7f3b246b99afc8bc6e591c8808a38556c400ad3c.tar.gz rails-7f3b246b99afc8bc6e591c8808a38556c400ad3c.tar.bz2 rails-7f3b246b99afc8bc6e591c8808a38556c400ad3c.zip |
Share foreign_key_present? implementation in _has_ associations
Diffstat (limited to 'activerecord/lib/active_record/associations')
3 files changed, 13 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/associations/foreign_association.rb b/activerecord/lib/active_record/associations/foreign_association.rb new file mode 100644 index 0000000000..fe48ecec29 --- /dev/null +++ b/activerecord/lib/active_record/associations/foreign_association.rb @@ -0,0 +1,11 @@ +module ActiveRecord::Associations + module ForeignAssociation + def foreign_key_present? + if reflection.klass.primary_key + owner.attribute_present?(reflection.active_record_primary_key) + else + false + end + end + end +end diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 93084e0dcf..d7f655d00c 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -6,6 +6,7 @@ module ActiveRecord # If the association has a <tt>:through</tt> option further specialization # is provided by its child HasManyThroughAssociation. class HasManyAssociation < CollectionAssociation #:nodoc: + include ForeignAssociation def handle_dependency case options[:dependent] @@ -153,14 +154,6 @@ module ActiveRecord end end - def foreign_key_present? - if reflection.klass.primary_key - owner.attribute_present?(reflection.association_primary_key) - else - false - end - end - def concat_records(records, *) update_counter_if_success(super, records.length) end diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index e6095d84dc..74b8c53758 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -2,6 +2,7 @@ module ActiveRecord # = Active Record Belongs To Has One Association module Associations class HasOneAssociation < SingularAssociation #:nodoc: + include ForeignAssociation def handle_dependency case options[:dependent] |