我已经遇到了一个问题,试图解决我将如何去编写我的游戏模型的一部分。在模型中陈述两条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