aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorTom Lea <contrib@tomlea.co.uk>2009-03-06 18:26:34 +0000
committerPratik Naik <pratiknaik@gmail.com>2009-03-06 18:27:34 +0000
commitc896d56c6e5520346c260477f7d7f8bf951e72cc (patch)
tree1e146c83008067d7c50d0b00f879679c36805dd8 /activerecord/lib
parent8bc0f90d6d5a6d653b9834e8bbca6ac20e25c293 (diff)
downloadrails-c896d56c6e5520346c260477f7d7f8bf951e72cc.tar.gz
rails-c896d56c6e5520346c260477f7d7f8bf951e72cc.tar.bz2
rails-c896d56c6e5520346c260477f7d7f8bf951e72cc.zip
Ensure self referential HABTM associations raise an exception if association_foreign_key is missing. [#1252 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb10
1 files changed, 10 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 6e88c89737..80b3e23026 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -51,6 +51,12 @@ module ActiveRecord
end
end
+ class HasAndBelongsToManyAssociationForeignKeyNeeded < ActiveRecordError #:nodoc:
+ def initialize(reflection)
+ super("Cannot create self referential has_and_belongs_to_many association on '#{reflection.class_name rescue nil}##{reflection.name rescue nil}'. :association_foreign_key cannot be the same as the :foreign_key.")
+ end
+ end
+
class EagerLoadPolymorphicError < ActiveRecordError #:nodoc:
def initialize(reflection)
super("Can not eagerly load the polymorphic association #{reflection.name.inspect}")
@@ -1526,6 +1532,10 @@ module ActiveRecord
options[:extend] = create_extension_modules(association_id, extension, options[:extend])
reflection = create_reflection(:has_and_belongs_to_many, association_id, options, self)
+
+ if reflection.association_foreign_key == reflection.primary_key_name
+ raise HasAndBelongsToManyAssociationForeignKeyNeeded.new(reflection)
+ end
reflection.options[:join_table] ||= join_table_name(undecorated_table_name(self.to_s), undecorated_table_name(reflection.class_name))