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

Ticket #11488: habtm_uniq_with_find.diff

File habtm_uniq_with_find.diff, 3.0 kB (added by cavalle, 3 months ago)
  • activerecord/test/cases/associations_test.rb

    old new  
    19451945    projects(:active_record).developers << developers(:david) 
    19461946    assert_equal 3, projects(:active_record, :reload).developers.size 
    19471947  end 
     1948   
     1949  def test_uniq_before_the_fact_find 
     1950    projects(:active_record).developers << developers(:jamis) 
     1951    assert_equal 3, projects(:active_record).developers.find(:all).size 
     1952  end 
     1953   
     1954  def test_uniq_before_the_fact_dynamic_find 
     1955    projects(:active_record).developers << developers(:jamis) 
     1956    assert_equal 2, projects(:active_record).developers.find_all_by_name('Jamis').size 
     1957  end 
    19481958 
    19491959  def test_deleting 
    19501960    david = Developer.find(1) 
     
    20062016  def test_additional_columns_from_join_table 
    20072017    assert_date_from_db Date.new(2004, 10, 10), Developer.find(1).projects.first.joined_on.to_date 
    20082018  end 
     2019   
     2020  def test_additional_columns_from_join_table_with_dynamic_find 
     2021    assert_date_from_db Date.new(2004, 10, 10), Developer.find(1).projects.find_by_name('Active Record').joined_on.to_date 
     2022  end 
    20092023 
    20102024  def test_destroy_all 
    20112025    david = Developer.find(1) 
  • activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb

    old new  
    5757 
    5858          merge_options_from_reflection!(options) 
    5959 
    60           options[:select] ||= (@reflection.options[:select] || '*') 
     60          distinct = 'DISTINCT ' if @reflection.options[:uniq] 
     61          options[:select] ||= (@reflection.options[:select] || construct_select) 
    6162 
    6263          # Pass through args exactly as we received them. 
    6364          args << options 
     
    137138                        :joins => @join_sql, 
    138139                        :readonly => false, 
    139140                        :order => @reflection.options[:order], 
    140                         :limit => @reflection.options[:limit] } } 
     141                        :limit => @reflection.options[:limit], 
     142                        :select => construct_select } } 
    141143        end 
     144         
     145        def construct_select 
     146          distinct          = 'DISTINCT ' if @reflection.options[:uniq] 
     147          association_table = @reflection.quoted_table_name 
     148          join_table        = @owner.connection.quote_table_name(@reflection.options[:join_table]) 
     149           
     150          "#{distinct}#{association_table}.*, #{join_table}.*" 
     151        end 
    142152 
    143153        # Join tables with additional columns on top of the two foreign keys must be considered ambiguous unless a select 
    144154        # clause has been explicitly defined. Otherwise you can get broken records back, if, for example, the join column also has