Skip to content

Commit

Permalink
Allow has_many :through associations to find the source association b…
Browse files Browse the repository at this point in the history
…y 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
  • Loading branch information
technoweenie committed Mar 19, 2006
1 parent 8ea1fad commit fcd4c95
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 11 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN* *SVN*


* Allow has_many :through associations to find the source association by setting a custom class (closes #4307) [jonathan@bluewire.net.nz]

* Eager Loading support added for has_many :through => :has_many associations (see below). [Rick Olson] * Eager Loading support added for has_many :through => :has_many associations (see below). [Rick Olson]


* Allow has_many :through to work on has_many associations (closes #3864) [sco@scottraymond.net] Example: * Allow has_many :through to work on has_many associations (closes #3864) [sco@scottraymond.net] Example:
Expand Down
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/associations.rb
Expand Up @@ -32,13 +32,13 @@ def message
end end


class HasManyThroughSourceAssociationNotFoundError < ActiveRecordError class HasManyThroughSourceAssociationNotFoundError < ActiveRecordError
def initialize(through_reflection, source_reflection_name) def initialize(through_reflection, source_reflection_names)
@through_reflection = through_reflection @through_reflection = through_reflection
@source_reflection_name = source_reflection_name @source_reflection_names = source_reflection_names
end end


def message 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
end end


Expand Down
19 changes: 12 additions & 7 deletions activerecord/lib/active_record/reflection.rb
Expand Up @@ -144,21 +144,26 @@ def through_reflection
@through_reflection ||= options[:through] ? active_record.reflect_on_association(options[:through]) : false @through_reflection ||= options[:through] ? active_record.reflect_on_association(options[:through]) : false
end end


def source_reflection_name # Gets an array of possible :through reflection names
@source_reflection_name ||= name.to_s.singularize.to_sym #
# [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 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 # class Post
# has_many :tags, :through => :taggings # has_many :tags, :through => :taggings
# end # end
# #
def source_reflection def source_reflection
return nil unless through_reflection return nil unless through_reflection
@source_reflection ||= \ @source_reflection ||= source_reflection_names.collect { |name| through_reflection.klass.reflect_on_association(name) }.compact.first
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
end end


def check_validity! def check_validity!
Expand All @@ -168,7 +173,7 @@ def check_validity!
end end


if source_reflection.nil? if source_reflection.nil?
raise HasManyThroughSourceAssociationNotFoundError.new(through_reflection, source_reflection_name) raise HasManyThroughSourceAssociationNotFoundError.new(through_reflection, source_reflection_names)
end end


if source_reflection.options[:polymorphic] if source_reflection.options[:polymorphic]
Expand Down
10 changes: 9 additions & 1 deletion activerecord/test/associations_join_model_test.rb
Expand Up @@ -55,7 +55,11 @@ def test_polymorphic_has_many_create_model_with_inheritance_and_custom_base_clas
def test_polymorphic_has_many_going_through_join_model_with_inheritance def test_polymorphic_has_many_going_through_join_model_with_inheritance
assert_equal tags(:general), posts(:thinking).tags.first assert_equal tags(:general), posts(:thinking).tags.first
end end


def test_polymorphic_has_many_going_through_join_model_with_inheritance_with_custom_class_name
assert_equal tags(:general), posts(:thinking).funky_tags.first
end

def test_polymorphic_has_many_create_model_with_inheritance def test_polymorphic_has_many_create_model_with_inheritance
post = posts(:thinking) post = posts(:thinking)
assert_instance_of SpecialPost, post assert_instance_of SpecialPost, post
Expand Down Expand Up @@ -234,6 +238,10 @@ def test_has_many_through_has_many_find_all
assert_equal comments(:greetings), authors(:david).comments.find(:all, :order => 'comments.id').first assert_equal comments(:greetings), authors(:david).comments.find(:all, :order => 'comments.id').first
end end


def test_has_many_through_has_many_find_all_with_custom_class
assert_equal comments(:greetings), authors(:david).funky_comments.find(:all, :order => 'comments.id').first
end

def test_has_many_through_has_many_find_first def test_has_many_through_has_many_find_first
assert_equal comments(:greetings), authors(:david).comments.find(:first, :order => 'comments.id') assert_equal comments(:greetings), authors(:david).comments.find(:first, :order => 'comments.id')
end end
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/fixtures/author.rb
Expand Up @@ -4,6 +4,7 @@ class Author < ActiveRecord::Base
has_many :posts_with_categories, :include => :categories, :class_name => "Post" has_many :posts_with_categories, :include => :categories, :class_name => "Post"
has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post" has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post"
has_many :comments, :through => :posts has_many :comments, :through => :posts
has_many :funky_comments, :through => :posts, :class_name => 'Comment'


has_many :special_posts, :class_name => "Post" has_many :special_posts, :class_name => "Post"
has_many :hello_posts, :class_name => "Post", :conditions=>"\#{aliased_table_name}.body = 'hello'" has_many :hello_posts, :class_name => "Post", :conditions=>"\#{aliased_table_name}.body = 'hello'"
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/fixtures/post.rb
Expand Up @@ -22,6 +22,7 @@ def find_most_recent


has_many :taggings, :as => :taggable has_many :taggings, :as => :taggable
has_many :tags, :through => :taggings has_many :tags, :through => :taggings
has_many :funky_tags, :through => :taggings, :class_name => 'Tag'
has_many :super_tags, :through => :taggings has_many :super_tags, :through => :taggings
has_one :tagging, :as => :taggable has_one :tagging, :as => :taggable


Expand Down

0 comments on commit fcd4c95

Please sign in to comment.