我有一个名为“匹配”的模型和一个名为“Bet”的模型
class Match < ActiveRecord::Base
...
has_many :bets
end
我的模特赌注:
class Bet < ActiveRecord::Base
attr_accessible :match_id, :user_id, :bet
...
belongs_to :match
belongs_to :user
end
我正在使用以下代码一起选择一些匹配和用户的赌注:
@matches = Match.includes(:bets).where("bets.user_id = ? or bets.user_id is NULL", @user.id)
如何通过此查询访问用户投注?
使用它不起作用:
@matches.each do |match|
match.bet.bet
...
如何在匹配中访问投注属性?
谢谢!
尝试@sevenseacat回答这段代码:
@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_id和user_id下注1次,所以它有效。)
答案 0 :(得分:2)
你只需在你的区块内match.bets
进行每次比赛的投注。
如果您想重复这些投注,请使用其他each
。
答案 1 :(得分:0)
@sevenseacat是对的
@match.bet.each { |bet| bet.attr }
对你有好处