【发布时间】:2013-03-18 11:15:19
【问题描述】:
如何显示 has_many_through 关联列表,关联作为列标题,通过:值作为表行中的条目?
我有 3 个模型:
class Jobs
attr_accesor :title
has_many :scores
has_many :factors, through: :scores
end
class Scores
attr_accesor :score
belongs_to :job
belongs_to :factor
end
class Factor
attr_accesor :name
has_many :scores
has_many :jobs, through: :scores
end
我希望能够在 Jobs 索引中显示每个 Job 的一行,每个 Factor 的标题作为列标题,每个 Job 的分数作为单元格中的值。
我希望必须在 app/admin/jobs.rb 文件中执行类似的操作:
index do |jobs|
column :title
jobs.scores.each do |score|
column(score.factor.name) { |score| score.score }
end
end
得到这样的输出:
Job | Education | Experience | Leadership | ... |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CEO | 600 | 720 | 580 | ... |
Admin Assistant | 210 | 200 | 150 | ... |
但 activeadmin 似乎不喜欢 jobs.scores.each 行,给我以下错误:
undefined method `scores' for
#<ActiveAdmin::Views::IndexAsTable::IndexTableFor:0x00000104d1dad0>
【问题讨论】:
-
我认为 Muhamamd Awais 是对的,我认为你需要改变你的方法。首先确保您遍历作业,因此您有 job.scores 因为它现在不知道要找到分数,除非您遍历每个单独的对象。其次,将其视为 erb 文件。 AA 中的所有内容都已打印 - 因此,当您说 job.scores.each 时,它实际上会像 而不是 。让我知道这是否有意义
-
谢谢Eileen,第二点是新的,我没有这么想。第一个是有道理的,但我无法弄清楚如何在
index块的上下文中循环作业以到达job.scores。在这方面的任何帮助都会很棒。
标签: ruby-on-rails ruby ruby-on-rails-3 activeadmin has-many-through