gpt4 book ai didi

ruby-on-rails - 在一个 View 表中对多个模型进行排序

转载 作者:行者123 更新时间:2023-12-02 21:18:32 25 4
gpt4 key购买 nike

我有一个模型,其中包含与其关联的各种不同类型的信息。

Opportunity.rb

has_many :notes
has_many :emails
has_many :calls

我目前在显示页面上有 3 个表,在我调用的 Controller 中

@notes = @opportunity.notes.sorted
@emails = @opportunity.emails.sorted
@calls = @opportunity.calls.sorted

并将它们显示在3个表格中

我想按时间顺序对它们进行排序,这样我就可以得到一个时间表。

每个模型都有日期,我想按日期排序,然后按日期顺序返回对象。IE。表格如下:

Note | Content | 11-Feb | Bob
Call | Content | 07-Feb | Dave
Email | Content | 04-Feb | Steve
Call | Content | 03-Feb | Dave
Note | Content | 01-Feb | Margaret

我无法弄清楚如何将 Controller 中的这些表合并到可以在 View 中使用的对象中,以便我可以创建一个表,按日期排序(“created_at”)并仍然保持一切看起来不错干净的。

最佳答案

最简单的方法是将这 3 个集合组合成一个数组,并按created_at 对其进行排序。在你的 Controller 中:

@combined = (@notes + @emails + @calls).sort_by {|record| record.created_at}

然后,您可以在 View 中使用 @combined 来显示时间线。

关于ruby-on-rails - 在一个 View 表中对多个模型进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29127611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com