lib/arrest/attributes/belongs_to.rb in arrest-0.0.67 vs lib/arrest/attributes/belongs_to.rb in arrest-0.0.68

- old
+ new

@@ -43,23 +43,25 @@ create_and_add_attribute(field_name, polymorphic, read_only, foreign_key, class_name) send :define_method, name do val = self.send(field_name) - if val == nil || val == "" + if val.blank? return nil end - begin - if polymorphic - clazz = self.class.json_type_to_class(val.type) - clazz.find(self.context, val.id) - else - Arrest::Source.mod.const_get(class_name).find(self.context, val) + @belongs_tos ||= {} + @belongs_tos[name] ||= + begin + if polymorphic + clazz = self.class.json_type_to_class(val.type) + clazz.find(self.context, val.id) + else + Arrest::Source.mod.const_get(class_name).find(self.context, val) + end + rescue Errors::DocumentNotFoundError => e + raise Errors::DocumentNotFoundError, "Couldnt find a #{class_name} with id #{val}" end - rescue Errors::DocumentNotFoundError => e - raise Errors::DocumentNotFoundError, "Couldnt find a #{class_name} with id #{val}" - end end end end end