Ruby on rails 检修轨道包括许多
我有一个叫“匹配”的模型和一个叫“打赌”的模型 如何使用此查询访问用户赌注 使用此选项不起作用:Ruby on rails 检修轨道包括许多,ruby-on-rails,include,has-many,Ruby On Rails,Include,Has Many,我有一个叫“匹配”的模型和一个叫“打赌”的模型 如何使用此查询访问用户赌注 使用此选项不起作用: @matches.each do |match| match.bet.bet ... 如何访问比赛中的下注属性 谢谢 正在使用以下代码尝试@sevensacat应答: @user ||= User.find_by_id(params[:user_id]) if params[:user_id] if @user @matches = Match.includes
@matches.each do |match|
match.bet.bet
...
如何访问比赛中的下注属性
谢谢
正在使用以下代码尝试@sevensacat应答:
@user ||= User.find_by_id(params[:user_id]) if params[:user_id]
if @user
@matches = Match.includes(:home_team, :away_team, :bets).where("bets.user_id = ? or bets.user_id is NULL", @user.id) #.group_by{ |match| match.date.strftime("%d/%m/%y")}
@matches.each do |match|
match.bets.each do |bet|
bet.bet
end
end
end
我已将其更改为match.bets.first(我对每个match\u id和user\u id只有1次下注,因此它可以正常工作)。您只需在区块内执行
match.bets
即可访问每一场比赛的下注
如果您想重复这些赌注,请使用另一个每个@sevensacat is right
@match.bet.each{| bet | bet.attr}
可能对你有好处谢谢你的回答。每个用户只能为一场比赛下注一次。有一种方法可以使用match.bet??根据您的代码,一场比赛仍然有许多赌注,但是match.bets
将只包含正确的赌注。此代码正在打印比赛对象,而不是赌注@matches.each do | match | match.bets.each do | bet | bet.bet end
该代码不会打印任何内容。您没有显示更多代码。我已将代码添加到post。我用的是葡萄宝石,这就是为什么它要打印一些东西
@matches = Match.includes(:bets).where("bets.user_id = ? or bets.user_id is NULL", @user.id)
@matches.each do |match|
match.bet.bet
...
@user ||= User.find_by_id(params[:user_id]) if params[:user_id]
if @user
@matches = Match.includes(:home_team, :away_team, :bets).where("bets.user_id = ? or bets.user_id is NULL", @user.id) #.group_by{ |match| match.date.strftime("%d/%m/%y")}
@matches.each do |match|
match.bets.each do |bet|
bet.bet
end
end
end