1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Don't perform statement caching for find when called from a scope

If there is a method defined such as `find_and_do_stuff(id)`, which then
gets called on an association, we will perform statement caching and the
parent ID will not change on subsequent calls.

Fixes #18117
This commit is contained in:
Sean Griffin 2014-12-22 15:38:58 -07:00
parent 18ae0656f5
commit fb160f6e7d
3 changed files with 32 additions and 1 deletions

View file

@ -131,6 +131,7 @@ module ActiveRecord
return super if block_given? ||
primary_key.nil? ||
default_scopes.any? ||
current_scope ||
columns_hash.include?(inheritance_column) ||
ids.first.kind_of?(Array)

View file

@ -15,9 +15,11 @@ require 'models/customer'
require 'models/toy'
require 'models/matey'
require 'models/dog'
require 'models/car'
require 'models/tyre'
class FinderTest < ActiveRecord::TestCase
fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :customers, :categories, :categorizations
fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :customers, :categories, :categorizations, :cars
def test_find_by_id_with_hash
assert_raises(ActiveRecord::StatementInvalid) do
@ -1101,6 +1103,26 @@ class FinderTest < ActiveRecord::TestCase
end
end
test "find on a scope does not perform statement caching" do
honda = cars(:honda)
zyke = cars(:zyke)
tyre = honda.tyres.create!
tyre2 = zyke.tyres.create!
assert_equal tyre, honda.tyres.custom_find(tyre.id)
assert_equal tyre2, zyke.tyres.custom_find(tyre2.id)
end
test "find_by on a scope does not perform statement caching" do
honda = cars(:honda)
zyke = cars(:zyke)
tyre = honda.tyres.create!
tyre2 = zyke.tyres.create!
assert_equal tyre, honda.tyres.custom_find_by(id: tyre.id)
assert_equal tyre2, zyke.tyres.custom_find_by(id: tyre2.id)
end
protected
def bind(statement, *vars)
if vars.first.is_a?(Hash)

View file

@ -1,3 +1,11 @@
class Tyre < ActiveRecord::Base
belongs_to :car
def self.custom_find(id)
find(id)
end
def self.custom_find_by(*args)
find_by(*args)
end
end