作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在进行复杂的查找时遇到了 will_paginate 的问题。
:photo has_many :tags, :through => :tagships
:item has_many :photos
:photo belongs_to :item
@photos = @item.photos.paginate :page => params[:page],
:per_page => 200,
:conditions => [ 'tags.id IN (?)', tag_ids],
:order => 'created_at DESC',
:joins => :tags,
:group => "photos.id HAVING COUNT(DISTINCT tags.id) = #{tag_count}"
SELECT count(*) AS count_all, photos.id HAVING COUNT(DISTINCT tags.id) = 1 AS photos_id_having_count_distinct_tags_id_1 FROM `photos`(...)
最佳答案
仅供引用,这是我最终发现解决此问题的方法:
@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
关于ruby-on-rails - rails : Using will_paginate with a complex association find,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2868484/
我是一名优秀的程序员,十分优秀!