Skip to content

Commit

Permalink
has_many :through conditions are sanitized by the associating class. C…
Browse files Browse the repository at this point in the history
…loses #5971.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4893 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Sep 1, 2006
1 parent 9c99be8 commit b1257d9
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 5 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/associations_join_model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'")
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions activerecord/test/fixtures/author.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions activerecord/test/fixtures/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/reflection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b1257d9

Please sign in to comment.