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

Fix stale_state for nested has_many :through associations

Need reloading when through record has replaced.
This commit is contained in:
Ryuta Kamizono 2018-01-10 22:13:21 +09:00
parent e97221a32d
commit 5a593486f5
3 changed files with 41 additions and 22 deletions

View file

@ -4,9 +4,20 @@ module ActiveRecord
module Associations
# = Active Record Through Association
module ThroughAssociation #:nodoc:
delegate :source_reflection, :through_reflection, to: :reflection
delegate :source_reflection, to: :reflection
private
def through_reflection
@through_reflection ||= begin
refl = reflection.through_reflection
while refl.through_reflection?
refl = refl.through_reflection
end
refl
end
end
# We merge in these scopes for two reasons:
#
@ -68,7 +79,7 @@ module ActiveRecord
end
def foreign_key_present?
through_reflection.belongs_to_or_through? && !owner[through_reflection.foreign_key].nil?
through_reflection.belongs_to? && !owner[through_reflection.foreign_key].nil?
end
def ensure_mutable

View file

@ -504,10 +504,6 @@ module ActiveRecord
@association_scope_cache.clear
end
def belongs_to_or_through?
belongs_to?
end
def nested?
false
end
@ -840,10 +836,6 @@ module ActiveRecord
source_reflection.join_scopes(table, predicate_builder) + super
end
def belongs_to_or_through?
through_reflection.belongs_to_or_through?
end
def has_scope?
scope || options[:source_type] ||
source_reflection.has_scope? ||

View file

@ -1314,12 +1314,20 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
has_many :subscriptions, through: :author
end
post = post_with_single_has_many_through.new
post.author = Author.create!(name: "Federico Morissette")
book = Book.create!(name: "essays on single has many through associations")
post.author.books << book
subscription = Subscription.first
book.subscriptions << subscription
assert_equal [subscription], post.subscriptions.to_a
post.author = authors(:mary)
book1 = Book.create!(name: "essays on single has many through associations 1")
post.author.books << book1
subscription1 = Subscription.first
book1.subscriptions << subscription1
assert_equal [subscription1], post.subscriptions.to_a
post.author = authors(:bob)
book2 = Book.create!(name: "essays on single has many through associations 2")
post.author.books << book2
subscription2 = Subscription.second
book2.subscriptions << subscription2
assert_equal [subscription2], post.subscriptions.to_a
end
def test_nested_has_many_through_association_with_unpersisted_parent_instance
@ -1329,12 +1337,20 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
has_many :subscriptions, through: :books
end
post = post_with_nested_has_many_through.new
post.author = Author.create!(name: "Obie Weissnat")
book = Book.create!(name: "essays on nested has many through associations")
post.author.books << book
subscription = Subscription.first
book.subscriptions << subscription
assert_equal [subscription], post.subscriptions.to_a
post.author = authors(:mary)
book1 = Book.create!(name: "essays on nested has many through associations 1")
post.author.books << book1
subscription1 = Subscription.first
book1.subscriptions << subscription1
assert_equal [subscription1], post.subscriptions.to_a
post.author = authors(:bob)
book2 = Book.create!(name: "essays on nested has many through associations 2")
post.author.books << book2
subscription2 = Subscription.second
book2.subscriptions << subscription2
assert_equal [subscription2], post.subscriptions.to_a
end
private