![](/img/trans.png)
[英]Rails “undefined method for ActiveRecord_Associations_CollectionProxy”
[英]Rails method on ActiveRecord::Associations::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
類的方法,但我無法弄清楚如何在類中的self方法中對結果進行迭代。
謝謝
我發現了。 使用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 }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.