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

  • SEARCH
  • Home
  • 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 7021225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:25:20+00:00 2026-05-27T23:25:20+00:00

My Thread model has many Posts . Let’s say I want to reorder posts

  • 0

My Thread model has many Posts. Let’s say I want to reorder posts by an array containg ids I wish to see my posts sorted by.

thread.posts.collect {|x| x.id} # => [1,2,3]
order = [2,3,1]

posts = thread.posts.sort_by {|x| order.index x.id}
posts.collect {|x| x.id} # => [2,3,1]

thread.update_attributes(:posts => posts) # => true
thread.posts.collect {|x| x.id} # => [1,2,3]

What am I doing wrong? Is sorting by id always preserved in collections and can I somehow disable it?

  • 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-05-27T23:25:20+00:00Added an answer on May 27, 2026 at 11:25 pm

    You should always assume the order of results retrieved from your database as being more or less “random”, unless you specifically ask it to sort them. This means that you can not rely on your database to magically store the order of posts associated with a thread (in fact, the code sample you posted would probably not query the database at all, because there is nothing to update).

    The easiest way to achieve what you want is to add an order field to your Post model like this:

    class AddOrderToPost < ActiveRecord::Migration
      def up
        change_table :posts do |t|
          t.integer :order, :default => 0
        end
        Post.update_all ["order = ?", 0]
      end
    
      def down
        remove_column :posts, :order
      end
    end
    

    In your Thread model:

    class Thread < ActiveRecord::Base
      # ...
      has_many :posts, :order => 'order ASC'
      # ...
    end
    

    Afterwards you will be able to reorder the posts like this:

    thread.posts.zip([2,3,1]) { |p,i| p.order = i }
    

    If you want, you can also use a plugin like acts_as_list which provides this and other useful functionality.

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

Sidebar

Related Questions

Say you have a Post model that has a :title , :author , :content
I have a Thread model which has a show page, and in that action's
I have a threads model, that has Thread (id, uuid, title...) Here is the
I have an application that has many views (Using tab bar controller). I want
Can Thread.Abort interrupt a thread that is sleeping (using, say, Thread.Sleep(TimeSpan.FromDays(40)) ? Or will
I was browsing this thread , which has good recommendation but a bit too
I'm writing an application that has a multiple producer, single consumer model (multiple threads
I'm having problems with updating a child model via nested form. I've read many
I have two models with a has-many/belongs-to relationship. The routes are nested, and I
My models are like this: a discussion has_many posts (nested resource). I want to

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.