Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix #3271.

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.

Conflicts:

	activerecord/test/cases/reflection_test.rb
  • Loading branch information...
commit d5e63b1a01eb38701fef1f56e341874db7c58bff 1 parent e8ce2a5
@jonleighton jonleighton authored
View
7 activerecord/CHANGELOG
@@ -1,5 +1,12 @@
*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.
View
2  activerecord/lib/active_record/reflection.rb
@@ -445,7 +445,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]
View
6 activerecord/test/cases/reflection_test.rb
@@ -326,6 +326,12 @@ def test_primary_key_name
end
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)
Please sign in to comment.
Something went wrong with that request. Please try again.