mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Don't find belongs_to target when the foreign_key is NULL. Fixes #2828
This commit is contained in:
parent
7f7e362c14
commit
2175012230
2 changed files with 10 additions and 0 deletions
|
@ -20,6 +20,10 @@ module ActiveRecord
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def find_target?
|
||||||
|
!loaded? && foreign_key_present? && klass
|
||||||
|
end
|
||||||
|
|
||||||
def update_counters(record)
|
def update_counters(record)
|
||||||
counter_cache_name = reflection.counter_cache_column
|
counter_cache_name = reflection.counter_cache_column
|
||||||
|
|
||||||
|
|
|
@ -352,6 +352,12 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
|
||||||
assert_equal members(:groucho), sponsor.sponsorable
|
assert_equal members(:groucho), sponsor.sponsorable
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_dont_find_target_when_foreign_key_is_null
|
||||||
|
tagging = taggings(:thinking_general)
|
||||||
|
queries = assert_sql { tagging.super_tag }
|
||||||
|
assert_equal 0, queries.length
|
||||||
|
end
|
||||||
|
|
||||||
def test_field_name_same_as_foreign_key
|
def test_field_name_same_as_foreign_key
|
||||||
computer = Computer.find(1)
|
computer = Computer.find(1)
|
||||||
assert_not_nil computer.developer, ":foreign key == attribute didn't lock up" # '
|
assert_not_nil computer.developer, ":foreign key == attribute didn't lock up" # '
|
||||||
|
|
Loading…
Reference in a new issue