aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2010-12-19 14:17:29 +0000
committerAaron Patterson <aaron.patterson@gmail.com>2010-12-23 15:19:17 -0800
commitc6db37e69b1ff07f7ad535d4752d0e6eb2d15bff (patch)
tree1f9540d4f14cf67b1277d8e7cd1f658561a79831 /activerecord/lib/active_record
parentd6efd3cfc2c6d6822aeac550852c49135fbe46c7 (diff)
downloadrails-c6db37e69b1ff07f7ad535d4752d0e6eb2d15bff.tar.gz
rails-c6db37e69b1ff07f7ad535d4752d0e6eb2d15bff.tar.bz2
rails-c6db37e69b1ff07f7ad535d4752d0e6eb2d15bff.zip
Don't allow a has_one association to go :through a collection association [#2976 state:resolved]
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations.rb6
-rw-r--r--activerecord/lib/active_record/reflection.rb4
2 files changed, 10 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index cdc8f25119..c0cd222244 100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -32,6 +32,12 @@ module ActiveRecord
end
end
+ class HasOneThroughCantAssociateThroughCollection < ActiveRecordError #:nodoc:
+ def initialize(owner_class_name, reflection, through_reflection)
+ super("Cannot have a has_one :through association '#{owner_class_name}##{reflection.name}' where the :through association '#{owner_class_name}##{through_reflection.name}' is a collection. Specify a has_one or belongs_to association in the :through option instead.")
+ end
+ end
+
class HasManyThroughSourceAssociationNotFoundError < ActiveRecordError #:nodoc:
def initialize(reflection)
through_reflection = reflection.through_reflection
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index 70165c600e..7a7f7812df 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -388,6 +388,10 @@ module ActiveRecord
raise HasManyThroughSourceAssociationMacroError.new(self)
end
+ if macro == :has_one && through_reflection.collection?
+ raise HasOneThroughCantAssociateThroughCollection.new(active_record.name, self, through_reflection)
+ end
+
check_validity_of_inverse!
end