Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7573715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:15:09+00:00 2026-05-30T16:15:09+00:00

I have run in to a problem at the moment on trying to solve

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:15:11+00:00Added an answer on May 30, 2026 at 4:15 pm

    Option 1) this is the method of least code:

    @games = Game
    @games = @games.where("game_name LIKE ?", "#{params[:game_name]}%") if params[:game_name].present?
    @games = @games.where("console = ?", params[:console]) if params[:console].present?
    

    however that will result in 3 queries.

    Option 2) 1 query, more code:

    @games = if params[:game_name].present? && params[:console].present?
      Game.where("console = ? AND game_name LIKE ?", params[:console], "#{params[:game_name]}%")
    elsif params[:game_name].present? && !params[:console].present?
      Game.where("game_name LIKE ?", "#{params[:game_name]}%")
    elsif !params[:game_name].present? && params[:console].present?
      Game.where("console = ?", params[:console])
    else
      Game
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have run into a problem trying to modify a form I myself have
I have run into a problem w/ my model for databinding in WPF. I
I have run into a problem... I'm trying to use QTKit in an application
I am trying to solve the following problem: Find the largest palindrome made from
I have run into a very interesting problem trying to debug my custom PHP
I have run into this problem a few times and I'm not happy with
it seems I have run into a problem with Internet Explorer 7. I have
I've just installed VS2008 and have run into a problem that I'm sure can
I am developing an xbap application and have run into a problem with users
I am new to jQuery and have run into a problem. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.