gpt4 book ai didi

ruby-on-rails - rails : Ordering from within a view

转载 作者:行者123 更新时间:2023-12-05 00:33:33 24 4
gpt4 key购买 nike

我正在使用acts_as_commentable_with_threading gem。这个 gem 包含方法“comment.children”,它创建所有子评论的散列。默认情况下,它通过 created_at ASC 对子项进行排序。我想改变 child 的排序方式,但据我所知,我不能直接编辑这个方法。相反,我一直试图在 View 中重新排序它们,如下所示:

<% @comments = comment.children.order('created_at DESC') %>

不幸的是,这没有效果。知道我做错了什么吗?

最佳答案

您应该使用 sort用于数组和哈希。

<% @comments = comment.children.sort { |a,b| b.created_at <=> a.created_at } %>

引用: http://apidock.com/ruby/Enumerable/sort

关于ruby-on-rails - rails : Ordering from within a view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11875174/

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