aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGareth Rees <gareth@mysociety.org>2015-05-13 14:48:31 +0100
committerGareth Rees <gareth@mysociety.org>2015-06-04 13:51:02 +0100
commit197dd8676b43dced1486feefde09dbd2d1d5d616 (patch)
tree601b6e061933fa25f59061bfef839bf8318f0b25
parent1184994308f61071262c9f43dbdfc926bbfb1dd0 (diff)
Use database query to return unique results
Use pluck to return only public_body_id instead of filtering in Ruby
-rw-r--r--app/models/public_body.rb11
1 files changed, 6 insertions, 5 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index 894e52985..6b512da36 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -169,11 +169,12 @@ class PublicBody < ActiveRecord::Base
# locales) return any of them
return found.first if found.size >= 1
- # If none found, then search the history of short names
- old = PublicBody::Version.find_all_by_url_name(name)
- # Find unique public bodies in it
- old = old.map { |x| x.public_body_id }
- old = old.uniq
+ # If none found, then search the history of short names and find unique
+ # public bodies in it
+ old = PublicBody::Version.
+ where(:url_name => name).
+ pluck('DISTINCT public_body_id')
+
# Maybe return the first one, so we show something relevant,
# rather than throwing an error?
raise "Two bodies with the same historical URL name: #{name}" if old.size > 1