1

I have the following models: User, Call.

A User has_many Calls.

A Call has a column: created_at

I need to filter the users who have the most calls in the last 'x' days.

I'm here so far:

User.includes(:calls).limit(10).where("created_at > ?", Time.now - 20.days)
Nizam
  • 5,698
  • 9
  • 45
  • 57
Alex D.
  • 21
  • 3

2 Answers2

0
User.includes(:calls).where("calls.created_at > ?", Time.now - 20.days).limit(10)
Thaha kp
  • 3,689
  • 1
  • 26
  • 25
0

You should go from Calls here, not from Users. Something like:

Call.where("created_at > ?", Time.now - 20.days).group(:user_id).order("count(user_id) DESC").count("user_id")

As a result you get a hash with key - user_id and value - number of calls

Andrey Sereda
  • 204
  • 2
  • 5