現時点で私のゲームモデルのセクションを書く方法を解決しようとすると、問題が発生しました。モデル内に2つのifステートメントを記述する
私は現在、モデルの私のデフインデックスセクションで、次があります。
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
今正常に動作しますが、それは次のものが含まれて私は、ユーザー名を見て、別のセクションに追加する:
user_relation = case params[:username].present?
when true then User.where("username LIKE ?", "#{params[:username]}%")
else User
end
今私はインデックスに2つのループを入れていると思いますが、@games行をどうやってやりますか?
私は次のようなものを試してみましたが、運:それはgames_relationとuser_relationを呼び出すように
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
私は現在、それがgames_relationを呼び出すが、どのようにしているが、私はそれを行うんでしょうか?
申し訳ありませんが、私はあなたの問題を理解していないが、一つのこと、私は確実に伝えることができます:あなたが唯一の2つのオプションがあります場合は、case文を使用しないでください。代わりに 'if ... else'を使用してください。 –
ちょっと私は、私が試したことと何をしようとしているのかを知るために質問を更新しました。 – user1222136
ゲームとユーザーの関係を記述することができれば、2秒後にこれを引き上げます。 – TomDunning