Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
Using Joins, Uniq and Group_by_month(:created_at)

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

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda