Forum: Ruby on Rails polymorphic,has_many through can not work?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2cf7af8ebb619eeb30178dd8855245b8?d=identicon&s=25 Charlie (Guest)
on 2006-04-21 04:37
Josh Susser 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...

I do that according to Josh Susser'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!
2cf7af8ebb619eeb30178dd8855245b8?d=identicon&s=25 Charlie (Guest)
on 2006-04-21 04:39
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
2cf7af8ebb619eeb30178dd8855245b8?d=identicon&s=25 Charlie (Guest)
on 2006-04-21 04:55
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
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-04-22 02:09
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 Susser
http://blog.hasmanythrough.com
This topic is locked and can not be replied to.