如何在Phoenix Framework中使用“直通”关系时查看相关模型?
例如,我有一个显示正常属性而没有任何问题的视图:
<%= @log.amount %>
我也与activities
有关系,定义如下:
has_many :log_activities, HelloPhoenix.LogActivity
has_many :activities, through: [:log_activities, :activity]
如何访问视图中的活动?
如果我致电@log.activities
,我会收到一项活动。如果我拨打@log.activities[0].name
或@log.activities.name
,我会收到错误(“参数错误”)
答案 0 :(得分:3)
To get the nth item out of a list, you need to use Enum.at(list, n)
and not list[n]
. For example, to get the name
of the first (index 0) item:
<%= Enum.at(@log.activities, 0).name %>