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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:28:08+00:00 2026-05-23T18:28:08+00:00

I am using Ruby on Rails 3.0.7 and I have a position database table

  • 0

I am using Ruby on Rails 3.0.7 and I have a position database table column in order to handle a sortable list of records. I would like to update that column for all records matching some conditions on destroying a record which position value is “in the middle of all position values”, but keeping the order (numerically) in the position column.

That is, for example, if in the following list of class objects I destroy the record with position 3 and user_id 1

#<Article id: 1, position: 1, user_id: 1>,
#<Article id: 2, position: 1, user_id: 2>,
#<Article id: 3, position: 2, user_id: 1>,
#<Article id: 4, position: 3, user_id: 1>,
#<Article id: 5, position: 1, user_id: 3>,
#<Article id: 6, position: 4, user_id: 1>,
#<Article id: 7, position: 5, user_id: 1>,
#<...> # A lot of others records having 'user_id' = 1

I would like to update all other records related to user_id 1 so to have

#<Article id: 1, position: 1, user_id: 1>,
#<Article id: 2, position: 1, user_id: 2>,
#<Article id: 3, position: 2, user_id: 1>,
#<Article id: 5, position: 1, user_id: 3>,
#<Article id: 6, position: 3, user_id: 1>,
#<Article id: 7, position: 4, user_id: 1>,
#<...> # A lot of others records having 'user_id' = 1

Of course, all that being careful to performance.

How can I do that (maybe in a single clause\statement)?


I know that the update_all method can help someway, but I don’t know how to auto-increment position values using that.

  • 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-23T18:28:09+00:00Added an answer on May 23, 2026 at 6:28 pm

    I guess what you are looking for in SQL is this?

    update articles set position = position - 1 
           where position > pos_of_deleted and user_id = uid_of_deleted
    

    With update_all it should convert to something like this:

    Article.update_all("position = position - 1", 
                       ["position > ? and user_id = ?", 
                        pos_of_deleted, uid_of_deleted])
    

    That will pull all positions above the deleted position down one notch..

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

Sidebar

Related Questions

I am using Ruby on Rails 3 and I would like to have a
I am using Ruby on Rails 3.1 and I would like to have an
I am using ruby on rails with a MySQL backend. I have a table
I am using Ruby on Rails 3.1 and I would like to know how
I'm using Ruby on Rails and i have a find_or_create_by_custom_stuff method. However i would
I am using ruby on rails and have my database setup as follows in
I am using Ruby on Rails 3 and I would like to know what
I am using Ruby on Rails 3 and I would like to increase the
I am using rails 3.1.3, postgreql9.1 and ruby 1.9.3. I would like to add
I am using Ruby on Rails 3.0.10 and I would like to subtract two

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.