I was wondering if there is a way to run a hook after records load with ActiveRecord::Relation.
I want to write a gem for including complex data in a rails query, for example
class User < ActiveRecord::Base
special_includes :profits do |result_set|
sales = Sale.select(“user_id, SUM(profit)”).group(:user_id).where(user_id: result_set.map(&:user_id)).to_h
result_set.each do |result|
result.profits = sales[result.user_id]
I want to do this to improve performance on complex pages with too much legacy code to completely refactor