aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2006-03-19 07:14:48 +0000
committerRick Olson <technoweenie@gmail.com>2006-03-19 07:14:48 +0000
commitfcd4c9529c071d3c77cbf57fe733869da0d3fde8 (patch)
tree76b40bc3ed6d210edfb33a7c09d428d160c12e10 /activerecord/lib
parent8ea1fadcbeefeb1b7f4bc46c083b0c761cde41e3 (diff)
downloadrails-fcd4c9529c071d3c77cbf57fe733869da0d3fde8.tar.gz
rails-fcd4c9529c071d3c77cbf57fe733869da0d3fde8.tar.bz2
rails-fcd4c9529c071d3c77cbf57fe733869da0d3fde8.zip
Allow has_many :through associations to find the source association by setting a custom class (closes #4307) [jonathan@bluewire.net.nz]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3973 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb6
-rw-r--r--activerecord/lib/active_record/reflection.rb19
2 files changed, 15 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index fb4895433d..2ed9e27e7f 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -32,13 +32,13 @@ module ActiveRecord
end
class HasManyThroughSourceAssociationNotFoundError < ActiveRecordError
- def initialize(through_reflection, source_reflection_name)
+ def initialize(through_reflection, source_reflection_names)
@through_reflection = through_reflection
- @source_reflection_name = source_reflection_name
+ @source_reflection_names = source_reflection_names
end
def message
- "Could not find the source association '#{@source_reflection_name}' in model #{@through_reflection.klass}"
+ "Could not find the source associations #{@source_reflection_names.to_sentence} in model #{@through_reflection.klass}"
end
end
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index 0d3333e0e1..a8e0227d40 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -144,11 +144,18 @@ module ActiveRecord
@through_reflection ||= options[:through] ? active_record.reflect_on_association(options[:through]) : false
end
- def source_reflection_name
- @source_reflection_name ||= name.to_s.singularize.to_sym
+ # Gets an array of possible :through reflection names
+ #
+ # [singularized, pluralized]
+ def source_reflection_names
+ @source_reflection_names ||= (options[:class_name] ?
+ [options[:class_name].underscore, options[:class_name].underscore.pluralize] :
+ [name.to_s.singularize, name]
+ ).collect { |n| n.to_sym }
end
- # Gets the source of the through reflection. (The :tags association on Tagging below)
+ # Gets the source of the through reflection. It checks both a singularized and pluralized form for :belongs_to or :has_many.
+ # (The :tags association on Tagging below)
#
# class Post
# has_many :tags, :through => :taggings
@@ -156,9 +163,7 @@ module ActiveRecord
#
def source_reflection
return nil unless through_reflection
- @source_reflection ||= \
- through_reflection.klass.reflect_on_association(source_reflection_name) || # has_many :through a :belongs_to
- through_reflection.klass.reflect_on_association(name) # has_many :through a :has_many
+ @source_reflection ||= source_reflection_names.collect { |name| through_reflection.klass.reflect_on_association(name) }.compact.first
end
def check_validity!
@@ -168,7 +173,7 @@ module ActiveRecord
end
if source_reflection.nil?
- raise HasManyThroughSourceAssociationNotFoundError.new(through_reflection, source_reflection_name)
+ raise HasManyThroughSourceAssociationNotFoundError.new(through_reflection, source_reflection_names)
end
if source_reflection.options[:polymorphic]