From 74dd1b21ec1bf3d3d046524f20c90252c0f67d02 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Fri, 20 Apr 2018 18:28:01 +0200 Subject: [PATCH] Fixed issue #1966: Paginated search results on Organizations can never be empty for queries that have at least one result (on an earlier page). --- app/models/organization/search.rb | 36 +++++++++++++++++-------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/app/models/organization/search.rb b/app/models/organization/search.rb index dc4f61e71..e6107536e 100644 --- a/app/models/organization/search.rb +++ b/app/models/organization/search.rb @@ -77,24 +77,28 @@ returns 'name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%" ).order('name').offset(offset).limit(limit).to_a - # if only a few organizations are found, search for names of users - if organizations.length <= 3 - organizations_by_user = Organization.select('DISTINCT(organizations.id), organizations.name').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').where( - 'users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%" - ).order('organizations.name').limit(limit) - organizations_by_user.each do |organization_by_user| - organization_exists = false - organizations.each do |organization| - if organization.id == organization_by_user.id - organization_exists = true - end - end + # use result independent of size if an explicit offset is given + # this is the case for e.g. paginated searches + return organizations if params[:offset].present? + return organizations if organizations.length > 3 - # get model with full data - if !organization_exists - organizations.push Organization.find(organization_by_user.id) - end + # if only a few organizations are found, search for names of users + organizations_by_user = Organization.select('DISTINCT(organizations.id), organizations.name').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').where( + 'users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%" + ).order('organizations.name').limit(limit) + + organizations_by_user.each do |organization_by_user| + + organization_exists = false + organizations.each do |organization| + next if organization.id != organization_by_user.id + organization_exists = true + break end + + # get model with full data + next if organization_exists + organizations.push Organization.find(organization_by_user.id) end organizations end