【发布时间】:2020-07-16 07:45:28
【问题描述】:
我正在开发自己的网络应用程序,但我遇到了问题。 这是我的模型:
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :player_seasons
has_many :forecasts, through: :player_seasons
end
class PlayerSeason < ApplicationRecord
belongs_to :user
belongs_to :season
belongs_to :championship
has_many :forecasts
has_many :matches, through: :forecasts
end
class Championship < ApplicationRecord
belongs_to :season
has_many :player_seasons
has_many :users, through: :player_seasons
end
@playerseasons = PlayerSeason.all
我正在迭代:@playerseasons 我想比较所有的 PlayerSeason (@playerseasons) 如下:
@playerseasons.each do |playerseason|
if playerseason.championship_id == @playerseason.championship
else
end
end
这个想法是比较所有 playerseason,如果 playerseason 与 current_user 拥有相同的冠军,则会出现其他内容(基本条件)。
但我卡住了,无法访问当前用户的确切冠军,另一方面我可以访问 playerseasons 的冠军。
提前谢谢你:)
【问题讨论】:
-
为什么无法访问 player.champsionship?这应该由设计加载为
current_user。 -
你好 Mark,当我在
@playerseasons上进行迭代时,我可以访问playerseason.championship。但是当我用@playerseason.championship尝试它时,它出现了未定义的方法“冠军” -
为什么不直接比较
@playerseason.championship_id和playerseason.championship_id? -
是的,这正是我正在做的事情,但是当我在做
@playerseason.championship_id时,它会引发一个未定义的方法错误:“#<:activerecord_relation:...> -
你是如何设置
@playerseason的?您不要在问题中发布它
标签: ruby-on-rails ruby postgresql