Improved organization search.

This commit is contained in:
Martin Edenhofer 2013-05-22 03:40:24 +02:00
parent 72df345203
commit 27d7172543
2 changed files with 16 additions and 5 deletions

View file

@ -21,8 +21,8 @@ class SearchController < ApplicationController
# do query # do query
users = User.search( users = User.search(
:query => params[:term], :query => params[:term],
:limit => params[:limit], :limit => params[:limit],
:current_user => current_user, :current_user => current_user,
) )
user_result = [] user_result = []
@ -32,10 +32,11 @@ class SearchController < ApplicationController
end end
organizations = Organization.search( organizations = Organization.search(
:query => params[:term], :query => params[:term],
:limit => params[:limit], :limit => params[:limit],
:current_user => current_user, :current_user => current_user,
) )
organizations_data = {} organizations_data = {}
organization_result = [] organization_result = []
organizations.each do |organization| organizations.each do |organization|

View file

@ -16,9 +16,19 @@ class Organization < ApplicationModel
organizations = Organization.find( organizations = Organization.find(
:all, :all,
:limit => limit, :limit => limit,
:conditions => ['name LIKE ?', "%#{query}%"], :conditions => ['name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%"],
:order => 'name' :order => 'name'
) )
# if only a few organizations are found, search for names of users
if organizations.length <= 3
organizations = Organization.select('DISTINCT(organizations.id)').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').find(
:all,
:limit => limit,
:conditions => ['users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%"],
:order => 'organizations.name'
)
end
return organizations return organizations
end end