From af13d48dfa88cfc0fb867b22dc19392d08b46364 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 18 Mar 2013 16:21:34 -0700 Subject: [PATCH] improved caching of root node for better performance --- lib/active_model/serializer/associations.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/active_model/serializer/associations.rb b/lib/active_model/serializer/associations.rb index a0851fe8..33b66ade 100644 --- a/lib/active_model/serializer/associations.rb +++ b/lib/active_model/serializer/associations.rb @@ -17,6 +17,14 @@ module ActiveModel end self.options = class_options + + # cache the root so we can reuse it without falling back on a per-instance basis + begin + self.options[:root] ||= self.new(name, nil).root + rescue + # this could fail if it needs a valid source, for example a polymorphic association + end + end end