aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2008-03-28 16:13:57 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2008-03-28 16:13:57 +0000
commitbdd88810c1edf8289801f88170eb5c9659b2f774 (patch)
tree5db49fa022d20885310a4e86bcac0496f601396e
parent533da24bbce5c6eeb23ff372fa0b65911fc138d0 (diff)
downloadrails-bdd88810c1edf8289801f88170eb5c9659b2f774.tar.gz
rails-bdd88810c1edf8289801f88170eb5c9659b2f774.tar.bz2
rails-bdd88810c1edf8289801f88170eb5c9659b2f774.zip
Fixed that has_many :through would ignore the hash conditions (closes #11447) [miloops]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9110 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb16
-rwxr-xr-xactiverecord/test/cases/associations_test.rb5
-rw-r--r--activerecord/test/models/author.rb5
4 files changed, 26 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index fe14ecd0f8..e8916b8d76 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fixed that has_many :through would ignore the hash conditions #11447 [miloops]
+
* Fix issue where the :uniq option of a has_many :through association is ignored when find(:all) is called. Closes #9407 [cavalle]
* Fix duplicate table alias error when including an association with a has_many :through association on the same join table. Closes #7310 [cavalle]
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index 083646f793..83c9865205 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -286,23 +286,35 @@ module ActiveRecord
def build_conditions
association_conditions = @reflection.options[:conditions]
- through_conditions = @reflection.through_reflection.options[:conditions]
+ through_conditions = build_through_conditions
source_conditions = @reflection.source_reflection.options[:conditions]
uses_sti = !@reflection.through_reflection.klass.descends_from_active_record?
if association_conditions || through_conditions || source_conditions || uses_sti
all = []
- [association_conditions, through_conditions, source_conditions].each do |conditions|
+ [association_conditions, source_conditions].each do |conditions|
all << interpolate_sql(sanitize_sql(conditions)) if conditions
end
+ all << through_conditions if through_conditions
all << build_sti_condition if uses_sti
all.map { |sql| "(#{sql})" } * ' AND '
end
end
+ def build_through_conditions
+ conditions = @reflection.through_reflection.options[:conditions]
+ if conditions.is_a?(Hash)
+ interpolate_sql(sanitize_sql(conditions)).gsub(
+ @reflection.quoted_table_name,
+ @reflection.through_reflection.quoted_table_name)
+ elsif conditions
+ interpolate_sql(sanitize_sql(conditions))
+ end
+ end
+
def build_sti_condition
"#{@reflection.through_reflection.quoted_table_name}.#{@reflection.through_reflection.klass.inheritance_column} = #{@reflection.klass.quote_value(@reflection.through_reflection.klass.name.demodulize)}"
end
diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb
index 0a05bd0e56..3c14ee0881 100755
--- a/activerecord/test/cases/associations_test.rb
+++ b/activerecord/test/cases/associations_test.rb
@@ -1324,6 +1324,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal 2, people(:michael).posts.find(:all, :include => :people).length
end
+ def test_has_many_through_respects_hash_conditions
+ assert_equal authors(:david).hello_posts, authors(:david).hello_posts_with_hash_conditions
+ assert_equal authors(:david).hello_post_comments, authors(:david).hello_post_comments_with_hash_conditions
+ end
+
end
class BelongsToAssociationsTest < ActiveRecord::TestCase
diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb
index 79a121b4a4..2918139f7f 100644
--- a/activerecord/test/models/author.rb
+++ b/activerecord/test/models/author.rb
@@ -35,6 +35,11 @@ class Author < ActiveRecord::Base
has_many :hello_post_comments, :through => :hello_posts, :source => :comments
has_many :posts_with_no_comments, :class_name => 'Post', :conditions => 'comments.id is null', :include => :comments
+ has_many :hello_posts_with_hash_conditions, :class_name => "Post",
+:conditions => {:body => 'hello'}
+ has_many :hello_post_comments_with_hash_conditions, :through =>
+:hello_posts_with_hash_conditions, :source => :comments
+
has_many :other_posts, :class_name => "Post"
has_many :posts_with_callbacks, :class_name => "Post", :before_add => :log_before_adding,
:after_add => :log_after_adding,