在视图中,如何通过 has_many 关系获取 Rails 中连接模型的 id?
如果我有两个模式加入有两个关系,可以说(团队和玩家)并且它们通过(阵容)加入,
有没有办法在调用类似以下内容后访问视图中的加入模型 id (lineups_id):
@teams.players.each_with_index |players, index| do
players.lineups_id
end
If I have two modals joined with a has many two relationship, lets say (teams and players) and they are joined through (lineups)
Is there a way to access the join model id (lineups_id) in the view after calling something like:
@teams.players.each_with_index |players, index| do
players.lineups_id
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,没有办法做到这一点。但通过稍微重新安排代码就可以很容易地解决这个问题。
.includes(:player)
实际上并不是必需的,但它会通过预先加载播放器来提高您的性能。As far as I know there's no way to do this. But it can be solved fairly easily by rearranging your code a bit.
The
.includes(:player)
isn't actually necessary but it will improve your performance by eager loading the players.