Polymorphic,has_many through can not work?


#1

Josh S. tells in his blog that the opposite direction of polymorphic
will get into trouble together with has_many through.
This is the url:
http://blog.hasmanythrough.com/articles/2006/04/03/polymorphic-through

I do that according to Josh S.'s procedure:

class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :taggable, :polymorphic => true
belongs_to :article, :class_name => “Article”,
:foreign_key => “taggable_id”,
:conditions => “taggable_type = ‘Article’”
belongs_to :user, :class_name => “User”,
:foreign_key => “taggable_id”,
:conditions => “taggable_type = ‘Book’”
end

class Tag < ActiveRecord::Base
has_many :taggings
has_many :articles, :through => :taggings, :source => :article
has_many :users, :through => :taggings, :source => :user
end

Then,according to the blog, Tag.find_by_name(“123”).articles
should get such SQL as:
SELECT articles.* FROM articles INNER JOIN taggings ON articles.id =
taggings.taggable_id WHERE (taggings.taggable_type = ‘Article’ AND
(taggings.tag_id = 1))

However, the “conditions” of the association can not work under my
enviroment:
I can only get SQL of this :
SELECT articles.* FROM articles INNER JOIN taggings ON articles.id =
taggings.taggable_id WHERE (taggings.tag_id = 1)

Therefore,wrong resultsets are returned. What is the matter with the
association? Anyone who can help me with the strange question?

Thanks!


#2

Sorry for my typing the question, the text body should be :

class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :taggable, :polymorphic => true
belongs_to :article, :class_name => “Article”,
:foreign_key => “taggable_id”,
:conditions => “taggable_type = ‘Article’”
belongs_to :user, :class_name => “User”,
:foreign_key => “taggable_id”,
:conditions => “taggable_type = ‘User’”
end


#3

Charlie wrote:

Even I write the :conditions in such words it can still work!

belongs_to :article, :class_name => “Article”,
:foreign_key => “taggable_id”,
:conditions => “bla bla bla”

It seems that :conditions option is omitted

You’re right, source conditions are ignored. How very odd. I posted a
reponse to your comment on my blog, but here it is as well:

@Charlie: My bad (I think). I could have sworn that code was working
when I posted this article, but it doesn’t now, even if I revert back to
the edge revision of that date. Anyway, if you move the :condition for
the type test to the Tag model from the Tagging model, it works
correctly and you get the expected SQL. (Don’t forget to qualify the
field with the table name.)

Check out the updated example in the article. It works. I even tested it
to make sure, heh.


Josh S.
http://blog.hasmanythrough.com


#4

Even I write the :conditions in such words it can still work!

belongs_to :article, :class_name => “Article”,
:foreign_key => “taggable_id”,
:conditions => “bla bla bla”

It seems that :conditions option is omitted

Charlie wrote:

Sorry for my typing the question, the text body should be :

class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :taggable, :polymorphic => true
belongs_to :article, :class_name => “Article”,
:foreign_key => “taggable_id”,
:conditions => “taggable_type = ‘Article’”
belongs_to :user, :class_name => “User”,
:foreign_key => “taggable_id”,
:conditions => “taggable_type = ‘User’”
end