2014-01-23 63 views
0

我有一个困境。在我的控制器中有一个巨大的功能来将不同类型的数据加载到视图的一个列表中。所以我有这样的此刻处理它的方式:ActiveRecord上的Rails方法::关联:: CollectionProxy

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 

我想移动,走出控制器到模型中,但我不太知道如何。我最好想要一个像customer.notes.format_for_timeline这样的方法,但我无法弄清楚如何在类中的自我方法中迭代结果。

谢谢

回答

1

我发现了如何。使用self方法则all

def self.format 
    all.each do |item| 
    # Manipulate items here 
    end 
end 

不过,我最后不得不像这样的方法:

def format 
    { 
    id: id, 
    note: 'Contact Note', 
    # Etc 
    } 
end 

然后,只需使用:

customer.notes.map {|i| i.format }

相关问题