Rails: Using will_paginate with a complex association find

后端 未结 2 1369
滥情空心
滥情空心 2020-12-30 16:23

I\'m encountering a problem with will_paginate while doing a complex find.

:photo  has_many   :tags,   :through => :tagships
:item   has_many   :photos
:p         


        
2条回答
  •  执笔经年
    2020-12-30 16:45

    My first stab at this (sorry don't have time to test it right now... will update if I do) would be something like the following (added the :select and changed the :group):

    @photos = @item.photos.paginate :page => params[:page],
                                    :per_page => 200,
                                    :select => "photos.*, COUNT(DISTINCT tags.id) AS tag_count",
                                    :conditions => [ 'tags.id IN (?)', tag_ids ],
                                    :order => 'created_at DESC',
                                    :joins => :tags,
                                    :group => "photos.id HAVING tag_count = #{tag_count}"
    

提交回复
热议问题