Hi so i am writing down the question again
so i have a 3 models 'User' 'Companion' 'Task'
and
User. has_many: companions, has_many: tasks
Companion: belongs_to: user
Task: belongs_to: user
and the datas what i need is a monthly data of User, and User who has Companions, then finally who has ordered a Task
so i was trying to use this codes
User.group_by_month(:created_at).count
Untill here i was able to have the datas what i was thinking
User.joins(:companions).uniq.group_by_month(:created_at).count
and i the monthly counted numbers of users who has companions but it doesn't work at all
then i also need to join a 'Task' to find out which user has a companion and also ordered a tasks
so the result what i would like to see is
Count User monthly registered
Aug, 2020 = > 300
Users who has companion
Aug, 2020 = > 150
Users who has companion and ordered a task
Aug, 2020 = > 75
I need uniq numbers User who has Companion, and User who has Companion and also Ordered a task
Uniq works on arrays. For a collection of records you can use distinct.
Check: https://apidock.com/rails/v5.2.3/ActiveRecord/QueryMethods/distinct
That means that you can use:
User.joins(:companions).distinct.group_by_month(:created_at).count