Class: Bulkrax::Status

Inherits:
ApplicationRecord show all
Defined in:
app/models/bulkrax/status.rb

Class Method Summary collapse

Class Method Details

.latest_by_statusable_subtableObject



14
15
16
17
18
19
20
21
22
23
# File 'app/models/bulkrax/status.rb', line 14

def self.latest_by_statusable_subtable
  status_table = self.arel_table
  latest_status_query = status_table.project(status_table[:statusable_id],
                                             status_table[:statusable_type],
                                             status_table[:id].maximum.as("latest_status_id")).group(status_table[:statusable_id], status_table[:statusable_type])

  latest_status_table = Arel::Table.new(latest_status_query).alias(:latest_status)
  status_table.join(latest_status_query.as(latest_status_table.name.to_s), Arel::Nodes::InnerJoin)
              .on(status_table[:id].eq(latest_status_table[:latest_status_id]))
end