faster album search

This commit is contained in:
Espen Antonsen 2009-08-12 15:42:18 +02:00
parent af7277ba82
commit 39f489d3bd

View file

@ -9,7 +9,9 @@ class AlbumsController < ApplicationController
#search = params[:q] #search = params[:q]
#search = search.split("AND").map{|q|q.strip} #search = search.split("AND").map{|q|q.strip}
#@albums = Album.find(:all, :select => 'DISTINCT albums.id, albums.title', :limit => 20, :conditions => { :tags => {:title => search}}, :joins => 'LEFT OUTER JOIN photos ON albums.id = photos.album_id LEFT OUTER JOIN photo_tags ON photos.id = photo_tags.photo_id LEFT OUTER JOIN tags ON photo_tags.tag_id = tags.id', :order => "albums.title ASC" ) #@albums = Album.find(:all, :select => 'DISTINCT albums.id, albums.title', :limit => 20, :conditions => { :tags => {:title => search}}, :joins => 'LEFT OUTER JOIN photos ON albums.id = photos.album_id LEFT OUTER JOIN photo_tags ON photos.id = photo_tags.photo_id LEFT OUTER JOIN tags ON photo_tags.tag_id = tags.id', :order => "albums.title ASC" )
@albums = Album.find(:all, :conditions => [ "id IN ( SELECT DISTINCT photos.album_id FROM photos WHERE photos.description LIKE :q OR photos.title LIKE :q OR photos.id IN ( SELECT photo_id FROM photo_tags LEFT OUTER JOIN tags ON photo_tags.tag_id = tags.id WHERE tags.title LIKE :q) )", { :q => '%' + params[:q] + '%' } ], :order => 'title') #@albums = Album.find(:all, :select => 'DISTINCT album_id', :conditions => [ "title LIKE :q OR description LIKE :q OR id IN ( SELECT DISTINCT photos.album_id FROM photos WHERE photos.description LIKE :q OR photos.title LIKE :q OR photos.id IN ( SELECT photo_id FROM photo_tags LEFT OUTER JOIN tags ON photo_tags.tag_id = tags.id WHERE tags.title LIKE :q) )", { :q => '%' + params[:q] + '%' } ], :order => 'title')
photos = Photo.find(:all, :select => 'DISTINCT album_id', :conditions => [ "description LIKE :q OR title LIKE :q OR id IN ( SELECT photo_id FROM photo_tags LEFT OUTER JOIN tags ON photo_tags.tag_id = tags.id WHERE tags.title LIKE :q)", { :q => '%' + params[:q] + '%' } ])
@albums = Album.find(:all, :conditions => ['title LIKE :q OR description LIKE :q OR id IN (:ids)', { :ids => photos.map{|p|p.album_id}, :q => '%' + params[:q] + '%' }], :order => 'title' )
else else
@albums = Album.find(:all, :order => 'title') @albums = Album.find(:all, :order => 'title')
end end
@ -45,6 +47,7 @@ class AlbumsController < ApplicationController
def create def create
@album = Album.new(params[:album]) @album = Album.new(params[:album])
if @album.save if @album.save
unless params[:collection_id].empty? unless params[:collection_id].empty?
@album.collections << Collection.find( params[:collection_id] ) @album.collections << Collection.find( params[:collection_id] )