印象派 gem - 按浏览量对帖子排序

Impressionist gem - sort posts by views

我正在使用以下 gem,现在我正在尝试按观看次数对 post 进行排序。

我在我的 post 模型中遵循了说明,所以我有:

is_impressionable :counter_cache => true

在我的控制器中,我有:

@mostpopular = @posts.order('counter_cache DESC').limit(6)

但是我收到一个错误:

SQLite3::SQLException: no such column: counter_cache: SELECT "posts".* FROM "posts" ORDER BY counter_cache DESC LIMIT 6

您是否在模型中添加了字段?

is_impressionable :counter_cache => true

This will automatically increment the impressions_count column in the included model. Note: You'll need to add that column to your model.

要添加你可以这样做:

t.integer :my_column_name, :default => 0

Read about this moment