Rails, Ruby, how to sort an Array?

后端 未结 6 1553
忘了有多久
忘了有多久 2021-01-31 07:13

in my rails app I\'m creating an array like so:

@messages.each do |message|

  @list << {
    :id => message.id,
    :title => message.title,
    :ti         


        
6条回答
  •  我在风中等你
    2021-01-31 07:47

    Just FYI, I don't see the point in moving the messages into a new list and then sorting them. As long as it is ActiveRecord it should be done directly when querying the database in my opinion.

    It looks like you should be able to do it like this:

    @messages = Message.includes(:replies).order("replies.created_at ASC")
    

    That should be enough unless I have misunderstood the purpose.

提交回复
热议问题