Browse files

has_many :through conditions are sanitized by the associating class. …

…Closes #5971.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4893 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 9c99be8 commit b1257d96b7b2482fc7811e5dc12d71c9fa091d10 @jeremy jeremy committed Sep 1, 2006
View
2 activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* has_many :through conditions are sanitized by the associating class. #5971 [martin.emde@gmail.com]
+
* Tighten rescue clauses. #5985 [james@grayproductions.net]
* Fix spurious newlines and spaces in AR::Base#to_xml output [Jamis Buck]
View
2 activerecord/lib/active_record/associations/association_proxy.rb
@@ -41,7 +41,7 @@ def aliased_table_name
end
def conditions
- @conditions ||= eval("%(#{@reflection.active_record.send :sanitize_sql, @reflection.options[:conditions]})") if @reflection.options[:conditions]
+ @conditions ||= eval("%(#{@reflection.klass.send :sanitize_sql, @reflection.options[:conditions]})") if @reflection.options[:conditions]
end
alias :sql_conditions :conditions
View
4 activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -207,8 +207,8 @@ def construct_sql
def conditions
@conditions ||= [
- (interpolate_sql(@reflection.active_record.send(:sanitize_sql, @reflection.options[:conditions])) if @reflection.options[:conditions]),
- (interpolate_sql(@reflection.active_record.send(:sanitize_sql, @reflection.through_reflection.options[:conditions])) if @reflection.through_reflection.options[:conditions])
+ (interpolate_sql(@reflection.klass.send(:sanitize_sql, @reflection.options[:conditions])) if @reflection.options[:conditions]),
+ (interpolate_sql(@reflection.through_reflection.klass.send(:sanitize_sql, @reflection.through_reflection.options[:conditions])) if @reflection.through_reflection.options[:conditions])
].compact.collect { |condition| "(#{condition})" }.join(' AND ') unless (!@reflection.options[:conditions] && !@reflection.through_reflection.options[:conditions])
end
View
4 activerecord/test/associations_join_model_test.rb
@@ -246,6 +246,10 @@ def test_has_many_find_all
def test_has_many_find_first
assert_equal categories(:general), authors(:david).categories.find(:first)
end
+
+ def test_has_many_with_hash_conditions
+ assert_equal categories(:general), authors(:david).categories_like_general.find(:first)
+ end
def test_has_many_find_conditions
assert_equal categories(:general), authors(:david).categories.find(:first, :conditions => "categories.name = 'General'")
View
6 activerecord/test/associations_test.rb
@@ -410,6 +410,10 @@ def test_finding_with_condition
assert_equal "Microsoft", Firm.find(:first).clients_like_ms.first.name
end
+ def test_finding_with_condition_hash
+ assert_equal "Microsoft", Firm.find(:first).clients_like_ms_with_hash_conditions.first.name
+ end
+
def test_finding_using_sql
firm = Firm.find(:first)
first_client = firm.clients_using_sql.first
@@ -1546,8 +1550,10 @@ def test_rich_association
def test_associations_with_conditions
assert_equal 3, projects(:active_record).developers.size
assert_equal 1, projects(:active_record).developers_named_david.size
+ assert_equal 1, projects(:active_record).developers_named_david_with_hash_conditions.size
assert_equal developers(:david), projects(:active_record).developers_named_david.find(developers(:david).id)
+ assert_equal developers(:david), projects(:active_record).developers_named_david_with_hash_conditions.find(developers(:david).id)
assert_equal developers(:david), projects(:active_record).salaried_developers.find(developers(:david).id)
projects(:active_record).developers_named_david.clear
View
2 activerecord/test/fixtures/author.rb
@@ -37,6 +37,8 @@ def testing_proxy_target
has_many :categorizations
has_many :categories, :through => :categorizations
+ has_many :categories_like_general, :through => :categorizations, :source => :category, :class_name => 'Category', :conditions => { :name => 'General' }
+
has_many :categorized_posts, :through => :categorizations, :source => :post
has_many :unique_categorized_posts, :through => :categorizations, :source => :post, :uniq => true
View
1 activerecord/test/fixtures/project.rb
@@ -3,6 +3,7 @@ class Project < ActiveRecord::Base
has_and_belongs_to_many :non_unique_developers, :order => 'developers.name desc, developers.id desc', :class_name => 'Developer'
has_and_belongs_to_many :limited_developers, :class_name => "Developer", :limit => 1
has_and_belongs_to_many :developers_named_david, :class_name => "Developer", :conditions => "name = 'David'", :uniq => true
+ has_and_belongs_to_many :developers_named_david_with_hash_conditions, :class_name => "Developer", :conditions => { :name => 'David' }, :uniq => true
has_and_belongs_to_many :salaried_developers, :class_name => "Developer", :conditions => "salary > 0"
has_and_belongs_to_many :developers_with_finder_sql, :class_name => "Developer", :finder_sql => 'SELECT t.*, j.* FROM developers_projects j, developers t WHERE t.id = j.developer_id AND j.project_id = #{id}'
has_and_belongs_to_many :developers_by_sql, :class_name => "Developer", :delete_sql => "DELETE FROM developers_projects WHERE project_id = \#{id} AND developer_id = \#{record.id}"
View
4 activerecord/test/reflection_test.rb
@@ -143,8 +143,8 @@ def test_association_reflection_in_modules
end
def test_reflection_of_all_associations
- assert_equal 14, Firm.reflect_on_all_associations.size
- assert_equal 12, Firm.reflect_on_all_associations(:has_many).size
+ assert_equal 15, Firm.reflect_on_all_associations.size
+ assert_equal 13, Firm.reflect_on_all_associations(:has_many).size
assert_equal 2, Firm.reflect_on_all_associations(:has_one).size
assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size
end

0 comments on commit b1257d9

Please sign in to comment.