Rails: Using will_paginate with a complex association find

后端 未结 2 1355
滥情空心
滥情空心 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:47

    FYI, here's what I finally found to fix this:

    @photos = WillPaginate::Collection.create(current_page, per_page) do |pager|
        result = @item.photos.find :all, :conditions => [ 'tags.id IN (?)', tag_ids] ,:order => 'created_at DESC', :joins => :tags, :group => "photos.id HAVING COUNT(DISTINCT tags.id) = #{@tags.count}", :limit => pager.per_page, :offset => pager.offset
        pager.replace(result)
    
        unless pager.total_entries
          pager.total_entries = @item.photos.find(:all, :conditions => [ 'tags.id IN (?)', tag_ids] ,:order => 'created_at DESC', :joins => :tags, :group => "photos.id HAVING COUNT(DISTINCT tags.id) = #{@tags.count}").count
        end
      end
    

    You have to manually construct the paginated set using the page number as an offset and using the tags to make a join query. Kinda clunky.

提交回复
热议问题