Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8235335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:46:19+00:00 2026-06-07T18:46:19+00:00

I have a Ruby array which contains Post objects (a Mongoid model) with a

  • 0

I have a Ruby array which contains Post objects (a Mongoid model) with a created_at method returning a Time object. What I want is to sort that array using that method (which is present on every element on the array). I tried @posts.sort_by {|post| post.created_at} but that didn’t work. How could I achieve this? Thanks!

EDIT: Example of the array:

[#<Post _id: 4ffd5184e47512e60b000003, _type: nil, created_at: 2012-07-11 10:12:20 UTC, title: "TestSecond", description: "TestSecond", slug: "assa", user_id: 4ffc4fd8e47512aa14000003>, #<Post _id: 4ffd518ee47512e60b000004, _type: nil, created_at: 2012-07-11 10:12:30 UTC, title: "TestFirst", description: "TestFirst", slug: "assadd", user_id: 4ffc4fd8e47512aa14000003>].

EDIT #2: I’m getting that @posts arra with:

@posts = Array.new

@user.following.each do |user|
   user.posts.each do |p|
    @posts << p
   end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:46:20+00:00Added an answer on June 7, 2026 at 6:46 pm

    I’m not sure why you have an array here. Normally, with Mongoid querying, you should have a Mongoid::Criteria instance, which you can desc(:created_by) or asc(:created_by) on, in order to sort.

    Still, can’t think of any reason sort_by doesn’t work for properly for you, works great on my app (just tried at console).

    UPD:

    @user.following.each do |user|
       user.posts.each do |p|
        @posts << p
       end
    end
    

    Well, to still have a mongoid collection there, something like this might be done:

    @posts = @user.following.map(&:posts).inject { |posts, post| posts.concat post }
    

    Now, you can @posts.desc(:created_at).

    UPD2:

    Also, for purpose of having a cleaner code, you could define a posts_from_following on User:

    class User
      def posts_from_following
        following.map(&:posts).inject { |posts, post| posts.concat post }
      end
    end
    

    and then, just do

    @posts = @user.posts_from_following.desc(:created_at)
    

    in your controller.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Ruby (1.9.2) array which I need to remove an object from.
I have got an array of objects in ruby and I want to iterate
I have a Ruby array which contains duplicate elements. array = [1,2,2,1,4,4,5,6,7,8,5,6] How can
I have an object Results that contains an array of result objects along with
I have a Ruby method that searches an array of hashes and returns a
I have an array of Ruby Date objects. How can I find the newest
Ruby 1.8.6 I have an array containing numerical values. I want to reduce it
I have two arrays which contain objects of assets, now I want to subtract
I have an array of ActiveRecord objects, each one which has its own respective
I have a String array which I want to convert to specifc format. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.