Skip to content

Commit

Permalink
Fix #3271.
Browse files Browse the repository at this point in the history
Building the conditions of a nested through association could
potentially modify the conditions of the through and/or source
association.

This is a Bad Thing.
  • Loading branch information
jonleighton committed Nov 3, 2011
1 parent 567d454 commit d486103
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
7 changes: 7 additions & 0 deletions activerecord/CHANGELOG
Expand Up @@ -39,6 +39,13 @@

*Rails 3.1.2 (unreleased)*

* Fix bug where building the conditions of a nested through association could potentially
modify the conditions of the through and/or source association. If you have experienced
bugs with conditions appearing in the wrong queries when using nested through associations,
this probably solves your problems. [GH #3271]

[Jon Leighton]

* If a record is removed from a has_many :through, all of the join records relating to that
record should also be removed from the through association's target.

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/reflection.rb
Expand Up @@ -433,7 +433,7 @@ def chain
# of relevant reflections, plus any :source_type or polymorphic :as constraints.
def conditions
@conditions ||= begin
conditions = source_reflection.conditions
conditions = source_reflection.conditions.map { |c| c.dup }

# Add to it the conditions from this reflection if necessary.
conditions.first << options[:conditions] if options[:conditions]
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/reflection_test.rb
Expand Up @@ -319,6 +319,12 @@ def test_foreign_key
assert_equal "category_id", Post.reflect_on_association(:categorizations).foreign_key.to_s
end

def test_through_reflection_conditions_do_not_modify_other_reflections
orig_conds = Post.reflect_on_association(:first_blue_tags_2).conditions.inspect
Author.reflect_on_association(:misc_post_first_blue_tags_2).conditions
assert_equal orig_conds, Post.reflect_on_association(:first_blue_tags_2).conditions.inspect
end

private
def assert_reflection(klass, association, options)
assert reflection = klass.reflect_on_association(association)
Expand Down

0 comments on commit d486103

Please sign in to comment.