ActiveRecord - querying polymorphic associations

前端 未结 8 1373
深忆病人
深忆病人 2020-12-04 14:10

I am using polymorphic associations to track Comments in my project. All very straight forward stuff.

The problem I have is in querying based on the polymorphic asso

8条回答
  •  余生分开走
    2020-12-04 14:55

    Argh!

    I think I found the problem.

    When joining via:

    @comments = Comment.find(:all,
            :joins => "forum_topics",
            :conditions => ["forum_topics.featured = ? ", true] 
            )
    

    You need the whole join!

    :joins => "INNER JOIN forum_topics ON forum_topics.id = comments.commentable_id",
    

    See the ever-awesome: http://guides.rubyonrails.org/active_record_querying.html#joining-tables

提交回复
热议问题