class ItemsController < ApplicationController
	helper_method :display_user_votes_for, :new_item_for_current_user

	def index
		@items = if params[:search] == nil
			Item.where(item_type_id: 1).includes(:user_votes).page params[:page]
		else
			Item.where(item_type_id: 1).where("lower(title) LIKE ?", "%#{params[:search].downcase}%").includes(:user_votes).page params[:page]
		end
	end

	def show 
		@item = Item.find params[:id]
		@user_vote = UserVote.new

		unless current_user == nil
			@past_vote = @item.user_votes.where(user_id: current_user.id, item_id: @item.id)
			@past_vote = @past_vote.first.vote unless @past_vote.count == 0 
		end
	end

	private 
	def new_item_for_current_user(item)
		item.user_votes.where("user_id = #{current_user.id}").empty?
	end
end

class ItemsController < ApplicationController
	helper_method :display_user_votes_for, :new_item_for_current_user

	def index
		@items = Item.where(item_type_id: 1).includes(:user_votes)

		@items = if params[:search] == nil
			@items.page params[:page]
		else
			@items.where("lower(title) LIKE ?", "%#{params[:search].downcase}%")
						.page params[:page]
		end
	end

	def show 
		@item = Item.find params[:id]
		@user_vote = UserVote.new

		unless current_user == nil
			@past_vote = @item.user_votes.where(user_id: current_user.id, item_id: @item.id)
			@past_vote = @past_vote.first.vote unless @past_vote.count == 0 
		end
	end

	private 
	def new_item_for_current_user(item)
		item.user_votes.where("user_id = #{current_user.id}").empty?
	end
end

Change Item Index Search To Use Least Code
Change Item Index Search To Use Least Code

Change Item index search to use lest code in order allow for updates in
the search for future updates.
