Skip to content

Commit 678dd80

Browse files
committed
Allow allocated Active Records to lookup associations
Previously, the association cache isn't setup on allocated record objects, so association lookups will crash. Test frameworks like mocha use allocate to check for stubbable instance methods, which can trigger an association lookup.
1 parent c3c3394 commit 678dd80

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Allow allocated Active Records to lookup associations.
2+
3+
Previously, the association cache isn't setup on allocated record objects, so association
4+
lookups will crash. Test frameworks like mocha use allocate to check for stubbable instance
5+
methods, which can trigger an association lookup.
6+
7+
*Gannon McGibbon*
8+
19
* Encryption now supports `support_unencrypted_data: true` being set per-attribute.
210

311
Previously this only worked if `ActiveRecord::Encryption.config.support_unencrypted_data == true`.

activerecord/lib/active_record/associations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def init_internals
7979

8080
# Returns the specified association instance if it exists, +nil+ otherwise.
8181
def association_instance_get(name)
82-
@association_cache[name]
82+
(@association_cache ||= {})[name]
8383
end
8484

8585
# Set the specified association instance.

activerecord/test/cases/associations_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def test_eager_loading_should_not_change_count_of_children
5959
assert_equal 1, liquids[0].molecules.length
6060
end
6161

62+
def test_allocated_record_can_see_assocations
63+
assert_not_nil Ship.allocate.association(:parts)
64+
end
65+
6266
def test_subselect
6367
author = authors :david
6468
favs = author.author_favorites

0 commit comments

Comments
 (0)