Skip to main content
# joins definidos en el método solo funcionarán si tienes definido:
has_many :authorisation_practices
has_many :authorisations, through: :authorisation_practices

def self.grouped_with_counters
  select(<<-SQL.squish
    SELECT practices.code, practices.name,
    SUM(
      CASE WHEN authorisations.created_at BETWEEN '#{Time.zone.now.beginning_of_month}' 
      AND '#{Time.zone.now}' THEN 1 ELSE 0 END
    ) AS COUNT_MES_ACTUAL,
    SUM(
      CASE WHEN authorisations.created_at BETWEEN '#{1.year.ago.beginning_of_month}' 
      AND '#{1.year.ago.end_of_month}' THEN 1 ELSE 0 END
    ) AS COUNT_1_YEAR_AGO,
    SUM(
      CASE WHEN authorisations.created_at BETWEEN '#{2.year.ago.beginning_of_month}' 
      AND '#{2.year.ago.end_of_month}' THEN 1 ELSE 0 END
    ) AS COUNT_2_YEAR_AGO
  SQL
  ).joins(:authorisations).where(authorisations: {state: :approved})
   .group(:code, :name).order(:name)
end
# joins definidos en el método solo funcionarán si tienes definido:
has_many :authorisation_practices
has_many :authorisations, through: :authorisation_practices

def self.grouped_with_counters
  select(<<-SQL.squish
    SELECT practices.code, practices.name,
    SUM(
      CASE WHEN authorisations.created_at BETWEEN '#{Time.zone.now.beginning_of_month}' 
      AND '#{Time.zone.now}' THEN 1 ELSE 0 END
    ) AS COUNT_MES_ACTUAL,
    SUM(
      CASE WHEN authorisations.created_at BETWEEN '#{1.year.ago.beginning_of_month}' 
      AND '#{1.year.ago.end_of_month}' THEN 1 ELSE 0 END
    ) AS COUNT_1_YEAR_AGO,
    SUM(
      CASE WHEN authorisations.created_at BETWEEN '#{2.year.ago.beginning_of_month}' 
      AND '#{2.year.ago.end_of_month}' THEN 1 ELSE 0 END
    ) AS COUNT_2_YEAR_AGO
  SQL
  ).joins(:authorisations).where(authorisations: {state: :approved})
   .group(:code, :name).order(:name)
end
# joins definidos en el método solo funcionarán si tienes definido:
has_many :authorisation_practices
has_many :authorisations, through: :authorisation_practices

def self.grouped_with_counters
  select(<<-SQL.squish
    practices.code, practices.name,
    SUM(
      CASE WHEN authorisations.created_at BETWEEN '#{Time.zone.now.beginning_of_month}' 
      AND '#{Time.zone.now}' THEN 1 ELSE 0 END
    ) AS COUNT_MES_ACTUAL,
    SUM(
      CASE WHEN authorisations.created_at BETWEEN '#{1.year.ago.beginning_of_month}' 
      AND '#{1.year.ago.end_of_month}' THEN 1 ELSE 0 END
    ) AS COUNT_1_YEAR_AGO,
    SUM(
      CASE WHEN authorisations.created_at BETWEEN '#{2.year.ago.beginning_of_month}' 
      AND '#{2.year.ago.end_of_month}' THEN 1 ELSE 0 END
    ) AS COUNT_2_YEAR_AGO
  SQL
  ).joins(:authorisations).where(authorisations: {state: :approved})
   .group(:code, :name).order(:name)
end
se añadieron 60 caracteres en el cuerpo
Origen Enlace
Alter Lagos
  • 5.5k
  • 2
  • 16
  • 27
# joins definidos en el método solo funcionarán si tienes definido:
has_many :authorisation_practices
has_many :authorisations, through: :authorisation_practices

def self.grouped_with_counters
  query = select(<<-SQL.squish
    SELECT practices.code, practices.name, 
    SUM(
      CASE WHEN authorisations.created_at BETWEEN ?'#{Time.zone.now.beginning_of_month}' 
      AND ?'#{Time.zone.now}' THEN 1 ELSE 0 END
    ) AS COUNT_MES_ACTUAL,
    SUM(
      CASE WHEN authorisations.created_at BETWEEN ? AND ? THEN '#{1 ELSE 0 END) AS.year.ago.beginning_of_month}' COUNT_1_YEAR_AGO,
    SUM(CASE WHEN authorisations.created_at BETWEEN ? AND ?'#{1.year.ago.end_of_month}' THEN 1 ELSE 0 END
    ) AS COUNT_2_YEAR_AGOCOUNT_1_YEAR_AGO,
  SQL

  selectSUM(
    query, Time.zone.now.beginning_of_month, Time.zone.now, 
 CASE WHEN authorisations.created_at BETWEEN 1'#{2.year.ago.beginning_of_month, 1.year.ago.end_of_month,}' 
    2.year.ago.beginning_of_month,  AND '#{2.year.ago.end_of_month}' THEN 1 ELSE 0 END
    ) AS COUNT_2_YEAR_AGO
  SQL
  ).joins(:authorisations).where(authorisations: {state: :approved})
   .group(:code, :name).order(:name)
end
# joins definidos en el método solo funcionarán si tienes definido:
has_many :authorisation_practices
has_many :authorisations, through: :authorisation_practices

def self.grouped_with_counters
  query = <<-SQL.squish
    SELECT practices.code, practices.name, 
    SUM(CASE WHEN authorisations.created_at BETWEEN ? AND ? THEN 1 ELSE 0 END) AS COUNT_MES_ACTUAL,
    SUM(CASE WHEN authorisations.created_at BETWEEN ? AND ? THEN 1 ELSE 0 END) AS COUNT_1_YEAR_AGO,
    SUM(CASE WHEN authorisations.created_at BETWEEN ? AND ? THEN 1 ELSE 0 END) AS COUNT_2_YEAR_AGO,
  SQL

  select(
    query, Time.zone.now.beginning_of_month, Time.zone.now, 
     1.year.ago.beginning_of_month, 1.year.ago.end_of_month, 
    2.year.ago.beginning_of_month,2.year.ago.end_of_month
  ).joins(:authorisations).where(authorisations: {state: :approved})
   .group(:code, :name).order(:name)
end
# joins definidos en el método solo funcionarán si tienes definido:
has_many :authorisation_practices
has_many :authorisations, through: :authorisation_practices

def self.grouped_with_counters
  select(<<-SQL.squish
    SELECT practices.code, practices.name,
    SUM(
      CASE WHEN authorisations.created_at BETWEEN '#{Time.zone.now.beginning_of_month}' 
      AND '#{Time.zone.now}' THEN 1 ELSE 0 END
    ) AS COUNT_MES_ACTUAL,
    SUM(
      CASE WHEN authorisations.created_at BETWEEN '#{1.year.ago.beginning_of_month}' 
      AND '#{1.year.ago.end_of_month}' THEN 1 ELSE 0 END
    ) AS COUNT_1_YEAR_AGO,
    SUM(
      CASE WHEN authorisations.created_at BETWEEN '#{2.year.ago.beginning_of_month}' 
      AND '#{2.year.ago.end_of_month}' THEN 1 ELSE 0 END
    ) AS COUNT_2_YEAR_AGO
  SQL
  ).joins(:authorisations).where(authorisations: {state: :approved})
   .group(:code, :name).order(:name)
end
Origen Enlace
Alter Lagos
  • 5.5k
  • 2
  • 16
  • 27

Eso lo puedes lograr con una única query con contadores. En tu controlador agrega:

# Creo que Practice debe ser el modelo principal que consultes y no la tabla
# que relaciona con las autorizaciones. Creo que debería tener una mejora en
# rendimiento para tu consulta
@practices = Practice.grouped_with_counters

y en el modelo Practice (o lo adaptas a un servicio como sugiere Gerry):

# joins definidos en el método solo funcionarán si tienes definido:
has_many :authorisation_practices
has_many :authorisations, through: :authorisation_practices

def self.grouped_with_counters
  query = <<-SQL.squish
    SELECT practices.code, practices.name, 
    SUM(CASE WHEN authorisations.created_at BETWEEN ? AND ? THEN 1 ELSE 0 END) AS COUNT_MES_ACTUAL,
    SUM(CASE WHEN authorisations.created_at BETWEEN ? AND ? THEN 1 ELSE 0 END) AS COUNT_1_YEAR_AGO,
    SUM(CASE WHEN authorisations.created_at BETWEEN ? AND ? THEN 1 ELSE 0 END) AS COUNT_2_YEAR_AGO,
  SQL

  select(
    query, Time.zone.now.beginning_of_month, Time.zone.now, 
    1.year.ago.beginning_of_month, 1.year.ago.end_of_month, 
    2.year.ago.beginning_of_month,2.year.ago.end_of_month
  ).joins(:authorisations).where(authorisations: {state: :approved})
   .group(:code, :name).order(:name)
end

luego en tu vista, iteras las prácticas con:

    <tbody>
      <% @practices.each do |practice| %>
        <tr>
          <td><%= practice.full_name %></td>
          <td class="text-center"><%= practice.count_mes_actual %></td>
          <td class="text-center">
            <%= number_to_currency(practice.count_media, unit: "") %>
          </td>
          <td class="text-center">
            <% unless practice.count_zero? %>
              <%= number_to_currency(practice.count_diff, unit: "%") %>
            <% end %>
          </td>
        </tr>
      <% end %>
    </tbody>

y esa lógica de la vista, la hice pensando que en tu modelo de Practice (o en un helper, o un presenter si es que estás aplicando el patrón en tu proyecto) tienes definidos los siguientes métodos:

def full_name
  "#{code} - #{name}"
end

def count_media
  (count_mes_actual + count_1_year_ago + count2_year_ago) / 3.0
end

def count_zero?
  count_mes_actual == 0 || count_media == 0.0
end

def count_diff
  (100 * (count_mes_actual - count_media) / count_media).to_i
end