I was just hoping to get some feedback to ensure that I'm understanding correctly. From what I've read, my understanding is that having multiple instances of where shouldn't hurt the performance of an application since the ActiveRecord relation objects themselves do not execute an SQL query until the chaining has been completed
In my case, say I have a table called Game and a table called Players. Games have many players, and players are involved in many games. I want to allow the user to type a name into a search box and search all of the games and filter them by the player name. If the user hasn't entered anything, it should return the list of all games where the attribute prospective == false
This code works fine for me functionally. I just want to ensure that from a performance standpoint, I'm not doing anything suboptimal.
def controller_name
games = Game.where.not(game: :prospective).order('score DESC')
if search_text.present?
games = games.joins(:players).where("lower(players.name) LIKE ?", "%#{search_text.downcase}%")
end
return games
end
In my case, I'm actually using GraphQL, but I don't think that should make a difference. I just wanted to get confirmation that this isn't going to execute 2 separate queries in that case that search_text.present? == true
I also welcome any other suggestions for optimizing or writing this more concisely. I'm fairly novice with rails