[英]Rails method on ActiveRecord::Associations::CollectionProxy
I have a dilema. 我有一个难题。 There's a huge function in my controller to standardise loads of different types of data into one list for the view.
我的控制器中有一个强大的功能,可以将不同类型的数据加载标准化到一个视图列表中。 So I have this kind of way of handling it at the moment:
因此,目前我有这种处理方式:
customer.notes.each do |note|
to_push = {
id: note.id,
title: 'Contact Note',
type: 'note',
description: note.notes,
user: note.user,
date: note.date,
action: nil,
extras: note.customer_interests,
closed: false,
colour: '#9b59b6'
}
history.push to_push
end
I want to move that out of the controller into the model but I'm not too sure how. 我想将其从控制器中移到模型中,但是我不太确定该怎么做。 I ideally want a method like
customer.notes.format_for_timeline
but I can't figure out how to iterate over results like that in a self method within the class. 理想情况下,我希望使用诸如
customer.notes.format_for_timeline
类的方法,但我无法弄清楚如何在类中的self方法中对结果进行迭代。
Thanks 谢谢
I found out how. 我发现了。 Using a
self
method then all
: 使用
self
方法则all
:
def self.format
all.each do |item|
# Manipulate items here
end
end
However, I ended up having a method like this: 但是,我最终有了这样的方法:
def format
{
id: id,
note: 'Contact Note',
# Etc
}
end
Then just used: 然后就用了:
customer.notes.map {|i| i.format }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.