aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb11
-rwxr-xr-xactiverecord/test/associations_test.rb13
-rw-r--r--activerecord/test/fixtures/project.rb1
4 files changed, 21 insertions, 6 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 695360b9ee..090258e07a 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fixed various problems with has_and_belongs_to_many when using customer finder_sql #2094 [Florian Weber]
+
* Added better exception error when unknown column types are used with migrations #1814 [fbeausoleil@ftml.net]
* Fixed "connection lost" issue with the bundled Ruby/MySQL driver (would kill the app after 8 hours of inactivity) #2163, #428 [kajism@yahoo.com]
diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
index d08e47378f..d386a5a906 100644
--- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -52,9 +52,9 @@ module ActiveRecord
ids = args.flatten.compact.uniq
if ids.size == 1
- id = ids.first
+ id = ids.first.to_i
record = load_target.detect { |record| id == record.id }
- expects_array? ? [record] : record
+ expects_array ? [record] : record
else
load_target.select { |record| ids.include?(record.id) }
end
@@ -95,10 +95,9 @@ module ActiveRecord
end
protected
- def find_target(sql = nil)
-
- if sql
- records = @association_class.find_by_sql(sql) if sql
+ def find_target
+ if @options[:finder_sql]
+ records = @association_class.find_by_sql(@finder_sql)
else
records = find(:all)
end
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 4eb6216dda..35e14a5e19 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -1145,6 +1145,19 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase
assert_equal developers(:david), active_record.developers.find(developers(:david).id), "Ruby find"
end
+ def test_find_in_association_with_custom_finder_sql
+ assert_equal developers(:david), projects(:active_record).developers_with_finder_sql.find(developers(:david).id), "SQL find"
+
+ active_record = projects(:active_record)
+ active_record.developers_with_finder_sql.reload
+ assert_equal developers(:david), active_record.developers_with_finder_sql.find(developers(:david).id), "Ruby find"
+ end
+
+ def test_find_in_association_with_custom_finder_sql_and_string_id
+ assert_equal developers(:david), projects(:active_record).developers_with_finder_sql.find(developers(:david).id.to_s), "SQL find"
+ end
+
+
def test_new_with_values_in_collection
jamis = DeveloperForProjectWithAfterCreateHook.find_by_name('Jamis')
david = DeveloperForProjectWithAfterCreateHook.find_by_name('David')
diff --git a/activerecord/test/fixtures/project.rb b/activerecord/test/fixtures/project.rb
index 9a13c9eead..f8519f1b8c 100644
--- a/activerecord/test/fixtures/project.rb
+++ b/activerecord/test/fixtures/project.rb
@@ -2,6 +2,7 @@ class Project < ActiveRecord::Base
has_and_belongs_to_many :developers, :uniq => true
has_and_belongs_to_many :developers_named_david, :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}"
has_and_belongs_to_many :developers_with_callbacks, :class_name => "Developer", :before_add => Proc.new {|o, r| o.developers_log << "before_adding#{r.id}"},
:after_add => Proc.new {|o, r| o.developers_log << "after_adding#{r.id}"},