我正在向我未曾使用过的旧应用程序添加功能,而RoR相对较新,这给我带来了一些困惑。
我有所谓的模型reponse
,activity_point
以及report
response
有两个父母,它属于activity_point
和report
。
我正在尝试访问activity_points
一个do块,如下所示:
report.responses.activity_points.activity.each do |a|
显然,这是行不通的。我收到错误消息:
undefined method `activity_points' for []:ActiveRecord::Relation
感谢任何可以帮助我解决这个小问题的人。
或者您可以在报告模型中添加类似的内容
has_many :responses
has_many :activity_points, :through => :responses
has_many :activities, :through => :activity_points
那么你可以做到这一点
report.activities.each do |a|
做这种事情的另一种方法是,在Report中添加一个方法,然后从另一端进行联接(以获取活动对象)
def activities
Activity.joins(:activity_points => :responses).where('responses.report_id = ?', id)
end
完成所有这些操作的目的是,如果不需要,您不想创建Ruby对象。嵌套循环也是唯一项和排序的潜在问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句