我已經遇到了一個問題,試圖解決我將如何去編寫我的遊戲模型的一部分。在模型中陳述兩條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
現在我想象我把兩個循環放入索引中,但是我會如何去做@games行呢?
我試圖像下面,但沒有運氣:
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但我會怎麼做,以便它調用games_relation和user_relation?
對不起,我聽不懂你的問題,但有一點我可以肯定地告訴:如果你只有兩個選擇,你不應該使用case語句。改用'if ... else'。 – 2012-03-01 11:34:23
嘿,我剛剛更新了我的問題,以瞭解我所嘗試的以及我正在嘗試做的事情。 – user1222136 2012-03-01 11:49:15
如果你只能描述遊戲和用戶之間的關係,那麼我會在2秒內爲你繪製它。 – TomDunning 2012-03-01 12:09:45