diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 8875b0db6..1e9b7b1f3 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -21,8 +21,8 @@ class SearchController < ApplicationController # do query users = User.search( - :query => params[:term], - :limit => params[:limit], + :query => params[:term], + :limit => params[:limit], :current_user => current_user, ) user_result = [] @@ -32,10 +32,11 @@ class SearchController < ApplicationController end organizations = Organization.search( - :query => params[:term], - :limit => params[:limit], + :query => params[:term], + :limit => params[:limit], :current_user => current_user, ) + organizations_data = {} organization_result = [] organizations.each do |organization| diff --git a/app/models/organization.rb b/app/models/organization.rb index 67747c88d..dc5f26451 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -16,9 +16,19 @@ class Organization < ApplicationModel organizations = Organization.find( :all, :limit => limit, - :conditions => ['name LIKE ?', "%#{query}%"], + :conditions => ['name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%"], :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 end