diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-05-10 13:46:37 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-05-10 13:51:31 +0200 |
commit | ed56e596a0467390011bc9d56d462539776adac1 (patch) | |
tree | 15a1746ac7c4d2a421085618b64e4d5c343ea154 /activerecord/lib | |
parent | f55f5e2b6d265764ff8ac23d56f768fda881f88f (diff) | |
download | rails-ed56e596a0467390011bc9d56d462539776adac1.tar.gz rails-ed56e596a0467390011bc9d56d462539776adac1.tar.bz2 rails-ed56e596a0467390011bc9d56d462539776adac1.zip |
deprecate, join, preload, eager load of instance dependent associations.
Closes #15024.
These operations happen before instances are created.
The current behavior is misleading and can result in broken behavior.
Diffstat (limited to 'activerecord/lib')
4 files changed, 20 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index d66a79a137..af6dabb793 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -419,6 +419,10 @@ module ActiveRecord # has_many :birthday_events, ->(user) { where starts_on: user.birthday }, class_name: 'Event' # end # + # Note: Joining, eager loading and preloading of these associations is not fully possibly. + # These operations happen before instance creation. The scope will be called with a +nil+ argument. + # This can lead to unexpected behavior and is deprecated. + # # == Association callbacks # # Similar to the normal callbacks that hook into the life cycle of an Active Record object, diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb index b7dc037a65..5842be3a7b 100644 --- a/activerecord/lib/active_record/associations/join_dependency.rb +++ b/activerecord/lib/active_record/associations/join_dependency.rb @@ -215,6 +215,7 @@ module ActiveRecord associations.map do |name, right| reflection = find_reflection base_klass, name reflection.check_validity! + reflection.check_eager_loadable! if reflection.options[:polymorphic] raise EagerLoadPolymorphicError.new(reflection) diff --git a/activerecord/lib/active_record/associations/preloader.rb b/activerecord/lib/active_record/associations/preloader.rb index 311684d886..20bd4947dc 100644 --- a/activerecord/lib/active_record/associations/preloader.rb +++ b/activerecord/lib/active_record/associations/preloader.rb @@ -175,6 +175,7 @@ module ActiveRecord if owners.first.association(reflection.name).loaded? return AlreadyLoaded end + reflection.check_preloadable! case reflection.macro when :has_many diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 7f4d77849a..0eec6774a0 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -278,6 +278,20 @@ module ActiveRecord end end + def check_preloadable! + return unless scope + + if scope.arity > 0 + ActiveSupport::Deprecation.warn <<-WARNING +The association scope '#{name}' is instance dependent (the scope block takes an argument). +Preloading happens before the individual instances are created. This means that there is no instance +being passed to the association scope. This will most likely result in broken or incorrect behavior. +Joining, Preloading and eager loading of these associations is deprecated and will be removed in the future. + WARNING + end + end + alias :check_eager_loadable! :check_preloadable! + def join_id_for(owner) #:nodoc: key = (source_macro == :belongs_to) ? foreign_key : active_record_primary_key owner[key] |