簡體   English   中英

ActiveRecord :: Associations :: CollectionProxy上的Rails方法

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM