根据日/周/月过滤acts_as_taggable tag_cloud

时间:2014-04-06 09:32:40

标签: sql ruby-on-rails postgresql ruby-on-rails-4 acts-as-taggable-on

我正在尝试让我的tag_cloud仅显示特定日期,周或月等的热门标记。

我试图通过将sql条件传递到tag_counts_on选项来使其工作。

例如,尝试为今天制作热门标签的tag_cloud

<% tag_cloud Micropost.tag_counts_on(:tags, options = {:conditions => ["SELECT * FROM microposts WHERE DATE(microposts.created_at) = DATE(NOW())"]}).limit(10).order('count desc'), %w[s m l] do |tag, css_class| %>
        <%= link_to tag.name, tag_path(tag.name), class: css_class%>

但是我收到以下错误

PG::Error: ERROR: subquery must return only one column LINE 1: ...0) AS taggings ON taggings.tag_id = tags.id WHERE (SELECT * ... ^ : SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "tags" JOIN (SELECT taggings.tag_id, COUNT(taggings.tag_id) AS tags_count FROM "taggings" INNER JOIN microposts ON microposts.id = taggings.taggable_id WHERE (taggings.taggable_type = 'Micropost' AND taggings.context = 'tags') AND (taggings.taggable_id IN(SELECT microposts.id FROM "microposts" ORDER BY created_at DESC)) GROUP BY taggings.tag_id HAVING COUNT(taggings.tag_id) > 0) AS taggings ON taggings.tag_id = tags.id WHERE (SELECT * FROM microposts WHERE DATE(microposts.created_at) = DATE(NOW())) LIMIT 10) subquery_for_count

不确定如何解决这个问题,或者这是否是尝试完成我想要做的事情的好方法。

1 个答案:

答案 0 :(得分:0)

我终于通过摆脱sql并使用:start_at选项for acts_as_taggable找到了http://rubydoc.info/github/mbleigh/acts-as-taggable-on/master/ActsAsTaggableOn/Taggable/Collection/ClassMethods

结果代码

Micropost.tag_counts_on(:tags, :limit => 5, :start_at => 1.day.ago )