aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGareth Rees <gareth@mysociety.org>2015-05-15 13:16:48 +0100
committerGareth Rees <gareth@mysociety.org>2015-06-04 13:51:18 +0100
commit0024aaef7547bb1657b51572f1c072e10db2d1e4 (patch)
tree99382c63ce1495345759ea260a62afd8a93ff048
parent98cf36fe876725a7ed8a11d5291e923f698ec5ae (diff)
Avoid loading associations to check existence
Calling InfoRequestEvent#is_X? loads the associated model from the database. We don't need to do this to check what 'type' the event is. This commit first checks whether there is an association_id – if there is then we know it's that 'type'. If there's no association_id, call the association to check whether one has been added but not yet persisted. Only check association if the object is a new record. This avoids checking the association on a persisted record when we know it isn't that type of event due to the database record not existing.
-rw-r--r--app/models/info_request_event.rb6
1 files changed, 3 insertions, 3 deletions
diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb
index 4a47edbe3..263de20a0 100644
--- a/app/models/info_request_event.rb
+++ b/app/models/info_request_event.rb
@@ -327,15 +327,15 @@ class InfoRequestEvent < ActiveRecord::Base
def is_incoming_message?
- !incoming_message.nil?
+ incoming_message_id? or (incoming_message if new_record?)
end
def is_outgoing_message?
- !self.outgoing_message.nil?
+ outgoing_message_id? or (outgoing_message if new_record?)
end
def is_comment?
- !self.comment.nil?
+ comment_id? or (comment if new_record?)
end
# Display version of status