This patch adds tests, documentation and code to support polymorphic belongs_to associations as the source in a has_many :through.
class Post < ActiveRecord::Base
has_many :taggings, :as => :taggable
end
class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :taggable, :polymorphic => true
end
class Tag < ActiveRecord::Base
has_many :taggings
has_many :tagged_posts, :through => :taggings, :source => :taggable, :source_type => 'Post'
end
Tag.find(:first).tagged_posts # returns a list of posts associated with this tag
Tag.find(:first, :include => :tagged_posts) # same, but eager loaded
Tag.find(:first).tagged_posts << Post.find(:first) # correctly sets taggable_id and taggable_type
It also supports fully-bidirectional polymorphic joins - where the join table looks like:
class Ownerships < ActiveRecord::Base
belongs_to :owner, :polymorphic => true
belongs_to :owned, :polymorphic => true
end