Impressionist gem - sort posts by views

192 views Asked by At

I'm using the following gem and now I'm trying to sort posts by the number of views.

I have followed the instructions and in my post model, so I have:

is_impressionable :counter_cache => true

And in my controller, I have:

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

But I'm getting an error:

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

1

There are 1 answers

0
Farkhat Mikhalko On BEST ANSWER

Did you added field to your model?

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.

To add you can do:

t.integer :my_column_name, :default => 0

Read about this moment