query optimisation

This commit is contained in:
Meutel 2015-08-04 19:32:38 +02:00
parent 76771541e9
commit 1030170c47
4 changed files with 20 additions and 9 deletions

View File

@ -16,8 +16,9 @@ class AuthorsController < ApplicationController
end
def index
@authors = Author.where(query_filter).order(:sort)
.paginate(page: params[:page], per_page: @preference.per_page)
@authors = Author.where(query_filter)
.order(:sort)
.paginate(pagination)
@title = "Authors"
respond_to do |format|
format.html

View File

@ -10,8 +10,10 @@ class BooksController < ApplicationController
end
def index
@books = Book.where(query_filter).order(sort_col)
.paginate(page: params[:page], per_page: @preference.per_page)
@books = Book .where(query_filter(Book.table_name))
.includes(:authors, :serie) .references(:authors, :serie)
.order(sort_col)
.paginate(pagination)
@title = "Books"
respond_to do |format|
format.html

View File

@ -9,8 +9,11 @@ class SeriesController < ApplicationController
end
def index
@series = Serie.where(query_filter).order(:sort)
.paginate(page: params[:page], per_page: @preference.per_page)
@series = Serie.where(query_filter(Serie.table_name))
.includes(books: :authors)
.references(:books)
.order(:sort)
.paginate(pagination)
@title = "Series"
respond_to do |format|
format.html

View File

@ -17,18 +17,23 @@ module PreferencesHelper
session[:preference] = @preference.to_json
end
def query_filter
def query_filter(prefix = nil)
sort_col = prefix ? prefix + ".sort" : "sort"
conditions = Array.new
params = Array.new
if @preference.initial
conditions.push "UPPER(sort) LIKE ?"
conditions.push "UPPER(#{sort_col}) LIKE ?"
params.push "#{@preference.initial}%"
end
if @preference.term
conditions.push "sort LIKE ?"
conditions.push "#{sort_col} LIKE ?"
params.push "%#{@preference.term}%"
end
[ conditions.join(" AND ") ] + params
end
def pagination
{ page: params[:page], per_page: @preference.per_page }
end
end