我想与用户相关,访问其预测并查看匹配的预测是否存在
I would like in relation with the user, access its forecasts and see if the forecast exists for the match
目标是在用户已经预测比赛的情况下将比赛隐藏在正面。
如果还没有开始,我将显示所有比赛,如果用户已经预测,我想显示或不显示。
为此,我正在使用:
@matches.each do |match|
if Time.parse(match.kick_off) > Time.now && ??
但是当我使用 current_user.forecasts 时,我访问了用户的所有预测,但是我无法将其与匹配项进行比较并查看是否存在特殊匹配项的预测。
class Match < ApplicationRecord
has_many :forecasts
has_many :season_matchs
end
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :player_seasons
has_many :forecasts, through: :player_seasons
end
class Forecast < ApplicationRecord
belongs_to :player_season
belongs_to :match
end
我终于自己找到了问题的答案。
对于那些有兴趣的人,我使用了 .find_by
current_user.forecasts.find_by(match_id: match.id).nil?
目标是在用户已经预测比赛的情况下将比赛隐藏在正面。 如果还没有开始,我将显示所有比赛,如果用户已经预测,我想显示或不显示。 为此,我正在使用:
@matches.each do |match|
if Time.parse(match.kick_off) > Time.now && ??
但是当我使用 current_user.forecasts 时,我访问了用户的所有预测,但是我无法将其与匹配项进行比较并查看是否存在特殊匹配项的预测。
class Match < ApplicationRecord
has_many :forecasts
has_many :season_matchs
end
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :player_seasons
has_many :forecasts, through: :player_seasons
end
class Forecast < ApplicationRecord
belongs_to :player_season
belongs_to :match
end
我终于自己找到了问题的答案。 对于那些有兴趣的人,我使用了 .find_by
current_user.forecasts.find_by(match_id: match.id).nil?