admitme_sam
admitme_sam

Reputation: 169

get popular records by id in rails

I have a table in my db that records the amount of times the page has been viewed by id.

The table records the event id and what im ideally wanting to do is count how many times the event id shows.

That way i can put the popular events on my homepage

Basically it looks like this

MY table:

ID
1
1
1
2
2
3
3
3
3

This way the returning result would be something like this:

3 would be first, 1 would be second and 2 would be third.

Then what i can do is each result would populate an each in the show on the view.

Any ideas how to do this?

Thanks!

Upvotes: 0

Views: 48

Answers (1)

Hiro
Hiro

Reputation: 359

In my memory I think

Model.group(:event_id).order(":count_event_id DESC").count(:event_id)

would work when I tried.

Upvotes: 1

Related Questions