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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:27:33+00:00 2026-06-18T21:27:33+00:00

I’m trying to manage an ActiveRecord model via its priority:integer column. I would like

  • 0

I’m trying to manage an ActiveRecord model via its priority:integer column. I would like to manage the rows with after_update and after_create hooks to keep their order neat and clean.

I have two needs:

  1. Take a current list of items and update their priority attribute to follow strict order.
    Example: a list of three items with said priority column.

    [a.priority = 4, b.priority = 66, c.priority = 92]
    

    becomes

    [a.priority = 1, b.priority = 2, c.priority = 3]
    
  2. Update all rows’ priority to reflect the addition of a new row in the middel of the list.

    [a.priority = 1, b.priority = 2, c.priority = 3, d.priority = 4]
    

    with an addition of e.priority = 2 create a new list of

    [a.priority = 1, e.priority = 2, b.priority = 3, c.priority = 4, d.priority = 5]
    

github repo: https://github.com/digitalcake/priority_manager

  • 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-18T21:27:34+00:00Added an answer on June 18, 2026 at 9:27 pm

    For the first case, you could do something like

    Model.order("priority ASC").each_with_index {|m,i| 
      m.update_attribute(:priority, i+1) }
    

    And the second one

    Model.where("priority >= ?", new_priority).each {|m| 
      m.update_attribute(:priority, m + 1) }
    

    That said, if you are only interested in the ordering and not the absolute position on the list it would be more efficient if instead of using integers to store your priorities, you used floats. Insert a row by assigning a it value between the priorities of the objects you want it to be in between. IE to insert a between b and c with respective priorities pb and pc assign it a priority of pa = (pc + pb) / 2

    This way the overall ordering remains intact, but you dont need to touch and resave every object with a higher priority every time you insert a new row.

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

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

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.