Let's say we have a ChessPiece class with the following migration.
create_table :chess_pieces do |t| t.string :type t.integer :row t.integer :column t.integer :game_id, index: true end
If a Bishop wanted to move from (0,0) to (7,7) then I need to check each space in between the two positions for other pieces that might be blocking my Bishop. IE if there was a Pawn on (5,5).
I could query the database for EACH position that might have a blocking piece like this:
pieces =  8.times do |i| pieces << ChessPiece.where(game_id: @game_id, row: i, column: i) end
But I want to cut down on queries. Alternatively, I could grab all the chess pieces for a game and iterate over them in Ruby, but that also seems inefficient. What I'd like to do is tell the database to combine the
column columns together into the form "row-column" and call it
position. That way I could run something like the following query:
ChessPiece.where(game_id: @game_id, position: ["1-1", "2-2", "3-3"])
How do I dynamically create this new column so I can run something like the above query? (Assume I can't just edit the database schema.)