if association is already loaded and if a find operation is performed on the already loaded association list with an empty hash then do not perform another sql
[#5972 state:resolved]
This commit is contained in:
parent
022519a032
commit
7c5c1a07c0
@ -75,6 +75,7 @@ def first(*args)
|
|||||||
find(:first, *args)
|
find(:first, *args)
|
||||||
else
|
else
|
||||||
load_target unless loaded?
|
load_target unless loaded?
|
||||||
|
args = args[1..-1] if args.first.kind_of?(Hash) && args.first.empty?
|
||||||
@target.first(*args)
|
@target.first(*args)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -544,7 +545,7 @@ def ensure_owner_is_not_new
|
|||||||
end
|
end
|
||||||
|
|
||||||
def fetch_first_or_last_using_find?(args)
|
def fetch_first_or_last_using_find?(args)
|
||||||
args.first.kind_of?(Hash) || !(loaded? || !@owner.persisted? || @reflection.options[:finder_sql] ||
|
(args.first.kind_of?(Hash) && !args.first.empty?) || !(loaded? || !@owner.persisted? || @reflection.options[:finder_sql] ||
|
||||||
!@target.all? { |record| record.persisted? } || args.first.kind_of?(Integer))
|
!@target.all? { |record| record.persisted? } || args.first.kind_of?(Integer))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -66,6 +66,17 @@ def test_create_from_association_should_respect_default_scope
|
|||||||
assert_equal 'exotic', bulb.name
|
assert_equal 'exotic', bulb.name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_no_sql_should_be_fired_if_association_already_loaded
|
||||||
|
car = Car.create(:name => 'honda')
|
||||||
|
bulb = car.bulbs.create
|
||||||
|
bulbs = Car.first.bulbs
|
||||||
|
bulbs.inspect # to load all instances of bulbs
|
||||||
|
assert_no_queries do
|
||||||
|
bulbs.first()
|
||||||
|
bulbs.first({})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_create_resets_cached_counters
|
def test_create_resets_cached_counters
|
||||||
person = Person.create!(:first_name => 'tenderlove')
|
person = Person.create!(:first_name => 'tenderlove')
|
||||||
post = Post.first
|
post = Post.first
|
||||||
|
Loading…
Reference in New Issue
Block a user