how to avoid calling .all
from this code? since .all
will be load all the items to memory and slow
I've tried to combine them but the code is not really that DRY
companies_controller.rb
def index
if params[:search].present?
@companies = Company.where("name ilike ?", "%#{params[:search]}%")
else
@companies = Company.all
end
if params[:page].present? && params[:per_page].present?
@companies = @companies.page(params[:page]).per(params[:per_page])
elsif params[:page].present?
@companies = @companies.page(params[:page])
else
@companies = @companies.page
end
end