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/polymorphic-through 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! -- Posted via http://www.ruby-forum.com/.
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-- Posted via http://www.ruby-forum.com/.
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-- Posted via http://www.ruby-forum.com/.
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 omittedYou''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 -- Posted via http://www.ruby-forum.com/.