I have run in to a problem at the moment on trying to solve how I would go about writing a section of my games model.
I currently have the following in my def index section of the model:
def index
games_relation = case params[:console].present?
when true then Game.where(:console => params[:console])
else Game
end
@games = games_relation.search(params[:search])
end
Now that works fine but I want to add in another section which looks at username so it includes the following:
user_relation = case params[:username].present?
when true then User.where("username LIKE ?", "#{params[:username]}%")
else User
end
Now I imagine I put in the two loops in to the index but how would I go about doing the @games line?
I have tried something like the following but no luck:
def index
games_relation = case params[:console].present?
when true then Game.where(:console => params[:console])
else Game
end
name_relation = case params[:game_name].present?
when true then Game.where("game_name LIKE ?", "#{params[:game_name]}%")
else Game
end
@games = name_relation.games_relation.search(params[:search])
end
I currently have it calling the games_relation but how would I do it so that it calls games_relation and the user_relation?
Option 1) this is the method of least code:
however that will result in 3 queries.
Option 2) 1 query, more code: