I want to do some operations on a relation if it has been eager laded (e.g. via includes). In concret terms, I have a data structure Thread which contains n Messages. I want to create a tree of the messages when they’re eager loaded. I tried it as follows:
class Thread
has_many :messages
after_initialize do
gen_tree if messages.loaded?
end
end
My Problem is that the hook is executed before the the relations have been loaded. Same with after_find.
Is there a way to achieve what I want?
Greetings,
CK
One solution I found is renaming the existing relation method and implement your own:
As far as I tested it, it has no negative side effects.
LG,
CK