开发者

In a view how do I get the id of a join model in rails with a has_many through relationship?

If I have two modals joined with a has many two rela开发者_运维问答tionship, 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


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.

@team.lineups.includes(:player).each_with_index |lineup, index| do
  lineup.id
  lineup.player
end

The .includes(:player) isn't actually necessary but it will improve your performance by eager loading the players.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜