# Polymorphic,has\_many through can not work?

**URL:** <https://www.ruby-forum.com/t/polymorphic-has-many-through-can-not-work/58393>\
**Category:** Rails\
**Created:** [April 21, 2006, 4:37am UTC](https://www.ruby-forum.com/t/polymorphic-has-many-through-can-not-work/58393 "2006-04-21T04:37:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [April 21, 2006, 4:37am UTC](https://www.ruby-forum.com/t/polymorphic-has-many-through-can-not-work/58393/1 "2006-04-21T04:37:33Z")

</div>

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](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!

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [April 21, 2006, 4:39am UTC](https://www.ruby-forum.com/t/polymorphic-has-many-through-can-not-work/58393/2 "2006-04-21T04:39:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [April 22, 2006, 2:09am UTC](https://www.ruby-forum.com/t/polymorphic-has-many-through-can-not-work/58393/3 "2006-04-22T02:09:46Z")

</div>

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](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [April 21, 2006, 4:55am UTC](https://www.ruby-forum.com/t/polymorphic-has-many-through-can-not-work/58393/4 "2006-04-21T04:55:15Z")

</div>

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
