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 1945 1945 projects(:active_record).developers << developers(:david) 1946 1946 assert_equal 3, projects(:active_record, :reload).developers.size 1947 1947 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 1948 1958 1949 1959 def test_deleting 1950 1960 david = Developer.find(1) … … 2006 2016 def test_additional_columns_from_join_table 2007 2017 assert_date_from_db Date.new(2004, 10, 10), Developer.find(1).projects.first.joined_on.to_date 2008 2018 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 2009 2023 2010 2024 def test_destroy_all 2011 2025 david = Developer.find(1) -
activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
old new 57 57 58 58 merge_options_from_reflection!(options) 59 59 60 options[:select] ||= (@reflection.options[:select] || '*') 60 distinct = 'DISTINCT ' if @reflection.options[:uniq] 61 options[:select] ||= (@reflection.options[:select] || construct_select) 61 62 62 63 # Pass through args exactly as we received them. 63 64 args << options … … 137 138 :joins => @join_sql, 138 139 :readonly => false, 139 140 :order => @reflection.options[:order], 140 :limit => @reflection.options[:limit] } } 141 :limit => @reflection.options[:limit], 142 :select => construct_select } } 141 143 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 142 152 143 153 # Join tables with additional columns on top of the two foreign keys must be considered ambiguous unless a select 144 154 # clause has been explicitly defined. Otherwise you can get broken records back, if, for example, the join column also has