diff options
author | Jan De Poorter <jan@defv.be> | 2008-09-22 18:14:42 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-09-22 18:22:30 +0100 |
commit | 050e58441bf7e60d167f6708072f8fa7aee2ce76 (patch) | |
tree | ab71b73bc91801cc2daa22ba92fb2da5ea1eec99 /activerecord/lib/active_record | |
parent | 46939a9b5a0098fddeac99a8a4331f66bdd0710e (diff) | |
download | rails-050e58441bf7e60d167f6708072f8fa7aee2ce76.tar.gz rails-050e58441bf7e60d167f6708072f8fa7aee2ce76.tar.bz2 rails-050e58441bf7e60d167f6708072f8fa7aee2ce76.zip |
Association#first and last should not load the association if not needed. [#1091 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index afb817f8ae..47a09510c8 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -63,7 +63,7 @@ module ActiveRecord # Fetches the first one using SQL if possible. def first(*args) - if fetch_first_or_last_using_find? args + if fetch_first_or_last_using_find?(args) find(:first, *args) else load_target unless loaded? @@ -73,7 +73,7 @@ module ActiveRecord # Fetches the last one using SQL if possible. def last(*args) - if fetch_first_or_last_using_find? args + if fetch_first_or_last_using_find?(args) find(:last, *args) else load_target unless loaded? @@ -420,7 +420,8 @@ module ActiveRecord end def fetch_first_or_last_using_find?(args) - args.first.kind_of?(Hash) || !(loaded? || @owner.new_record? || @reflection.options[:finder_sql] || !@target.blank? || args.first.kind_of?(Integer)) + args.first.kind_of?(Hash) || !(loaded? || @owner.new_record? || @reflection.options[:finder_sql] || + @target.any? { |record| record.new_record? } || args.first.kind_of?(Integer)) end end end |