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 9026619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:33:58+00:00 2026-06-16T06:33:58+00:00

I have a rails model let say User with a has many relationship with

  • 0

I have a rails model let say
User with a has many relationship with Post. Post contains an attribute named position that stores the position (acts_as_list gem). The position stores the position on the user Post list (the order is not due to creation/modification of the post).
Moreover, the Post has timestamps stored (t.timestamps in the migration)

So I have defined the following in User.rb:

has_many :post, :order => "position", :dependent => :destroy

However, when i do a User.first.posts The posts are not ordered by position.
I’ve cheked and the sql being queries contains the following:

ORDER BY post.created_at DESC, position

So the created_at is still being used.

How can I sort by position and not use the created_at attribute?

Thanks in advance!

SOLUTION
I had a default scope degined in Post that was creating the undesired order by.

Problem solved

  • 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-16T06:34:00+00:00Added an answer on June 16, 2026 at 6:34 am

    You can use a default scope:

    class Post < ActiveRecord::Base
      scope :by_position, order("position ASC")
      default_scope by_position
      #...
    end
    

    Then @user.posts should return a list ordered by the position (ASC).

    Also, the following should be working for you: (plural on post)

    has_many :posts, :dependent => :destroy, :order => "posts.position ASC"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user model that has many fields (let's say 30 for this
Let's say I have a Ruby on Rails blogging application with a Post model.
Beginner rails/javascript question: Let's say that I have a simple Circle model in a
Let's say I have a User model which has_many :posts , while Post has_many
I have a Ruby on Rails model that has a column called expiration_date .
I have a rails app (rails 3.1.3) that has a shopping cart model. I
I have many instances of a Rails model, Post . When viewing an individual
I'm using Rails 3 with Devise for user auth. Let's say I have a
Let's say I have a model foo, and my model has a publish! method
Let's say I have a controller and model called Car/Cars in Rails. Then I

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.