Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4893

Show
Ignore:
Timestamp:
09/01/06 05:31:56 (2 years ago)
Author:
bitsweat
Message:

has_many :through conditions are sanitized by the associating class. Closes #5971.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r4885 r4893  
    11*SVN* 
     2 
     3* has_many :through conditions are sanitized by the associating class.  #5971 [martin.emde@gmail.com] 
    24 
    35* Tighten rescue clauses.  #5985 [james@grayproductions.net] 
  • trunk/activerecord/lib/active_record/associations/association_proxy.rb

    r4791 r4893  
    4242       
    4343      def conditions 
    44         @conditions ||= eval("%(#{@reflection.active_record.send :sanitize_sql, @reflection.options[:conditions]})") if @reflection.options[:conditions] 
     44        @conditions ||= eval("%(#{@reflection.klass.send :sanitize_sql, @reflection.options[:conditions]})") if @reflection.options[:conditions] 
    4545      end 
    4646      alias :sql_conditions :conditions 
  • trunk/activerecord/lib/active_record/associations/has_many_through_association.rb

    r4792 r4893  
    208208        def conditions 
    209209          @conditions ||= [ 
    210             (interpolate_sql(@reflection.active_record.send(:sanitize_sql, @reflection.options[:conditions])) if @reflection.options[:conditions]), 
    211             (interpolate_sql(@reflection.active_record.send(:sanitize_sql, @reflection.through_reflection.options[:conditions])) if @reflection.through_reflection.options[:conditions]) 
     210            (interpolate_sql(@reflection.klass.send(:sanitize_sql, @reflection.options[:conditions])) if @reflection.options[:conditions]), 
     211            (interpolate_sql(@reflection.through_reflection.klass.send(:sanitize_sql, @reflection.through_reflection.options[:conditions])) if @reflection.through_reflection.options[:conditions]) 
    212212          ].compact.collect { |condition| "(#{condition})" }.join(' AND ') unless (!@reflection.options[:conditions] && !@reflection.through_reflection.options[:conditions]) 
    213213        end 
  • trunk/activerecord/test/associations_join_model_test.rb

    r4792 r4893  
    246246  def test_has_many_find_first 
    247247    assert_equal categories(:general), authors(:david).categories.find(:first) 
     248  end 
     249 
     250  def test_has_many_with_hash_conditions 
     251    assert_equal categories(:general), authors(:david).categories_like_general.find(:first) 
    248252  end 
    249253   
  • trunk/activerecord/test/associations_test.rb

    r4848 r4893  
    409409  def test_finding_with_condition 
    410410    assert_equal "Microsoft", Firm.find(:first).clients_like_ms.first.name 
     411  end 
     412 
     413  def test_finding_with_condition_hash 
     414    assert_equal "Microsoft", Firm.find(:first).clients_like_ms_with_hash_conditions.first.name 
    411415  end 
    412416 
     
    15471551    assert_equal 3, projects(:active_record).developers.size 
    15481552    assert_equal 1, projects(:active_record).developers_named_david.size 
     1553    assert_equal 1, projects(:active_record).developers_named_david_with_hash_conditions.size 
    15491554 
    15501555    assert_equal developers(:david), projects(:active_record).developers_named_david.find(developers(:david).id) 
     1556    assert_equal developers(:david), projects(:active_record).developers_named_david_with_hash_conditions.find(developers(:david).id) 
    15511557    assert_equal developers(:david), projects(:active_record).salaried_developers.find(developers(:david).id) 
    15521558 
  • trunk/activerecord/test/fixtures/author.rb

    r4372 r4893  
    3737  has_many :categorizations 
    3838  has_many :categories, :through => :categorizations 
     39 
     40  has_many :categories_like_general, :through => :categorizations, :source => :category, :class_name => 'Category', :conditions => { :name => 'General' } 
    3941 
    4042  has_many :categorized_posts, :through => :categorizations, :source => :post 
  • trunk/activerecord/test/fixtures/project.rb

    r4326 r4893  
    44  has_and_belongs_to_many :limited_developers, :class_name => "Developer", :limit => 1 
    55  has_and_belongs_to_many :developers_named_david, :class_name => "Developer", :conditions => "name = 'David'", :uniq => true 
     6  has_and_belongs_to_many :developers_named_david_with_hash_conditions, :class_name => "Developer", :conditions => { :name => 'David' }, :uniq => true 
    67  has_and_belongs_to_many :salaried_developers, :class_name => "Developer", :conditions => "salary > 0" 
    78  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}' 
  • trunk/activerecord/test/reflection_test.rb

    r4291 r4893  
    144144   
    145145  def test_reflection_of_all_associations 
    146     assert_equal 14, Firm.reflect_on_all_associations.size 
    147     assert_equal 12, Firm.reflect_on_all_associations(:has_many).size 
     146    assert_equal 15, Firm.reflect_on_all_associations.size 
     147    assert_equal 13, Firm.reflect_on_all_associations(:has_many).size 
    148148    assert_equal 2, Firm.reflect_on_all_associations(:has_one).size 
    149149    assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size