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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:15:52+00:00 2026-05-30T23:15:52+00:00

I have a simple work schedule rails app. The jobs in the schedule will

  • 0

I have a simple work schedule rails app. The jobs in the schedule will be ordered by the priority of the job. Since some jobs may change in priority, and by the same nature, order they need to be completed, i need to be able to update the priority and the priority of the rest of the table to ensure no 2 jobs share the same priority. Once the priority is updated, i want it to make that the list of priorities is continuous, in other words, the priorities are labeled 1,2,3,4,5,etc. instead of having gaps like 1,2,4,5,6,8,etc.

Can someone help me figure out the proper coding to achieve this?

This is what i currently have in my model:

class Job < ActiveRecord::Base
  include ActiveModel::Dirty
  belongs_to :customer
  has_many :job_items

  before_validation :update_priorities

  validates :priority, :uniqueness => true

  private

  def update_priorities
  if self.priority_changed?
    self.class.where("priority >= ?", self.priority).update_all("priority = priority + 1")
  else
    nil  
  end
end

The above code updates the priorities fine if its a brand new job. However, once i start to re-order the current jobs, gaps begin to appear in the sequence.

I am currently using Rails 3.2.1

  • 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-30T23:15:53+00:00Added an answer on May 30, 2026 at 11:15 pm

    I tried the acts_as_list provided by Veraticus, but it just wasn’t working properly for my application. After some fiddling with it, i changed my code to the following and it works just like i need it to.

      def update_priorities
    if self.priority_changed?
      if self.priority < self.priority_was
        self.class.where("priority >= ?", self.priority).update_all("priority = priority + 1")
        self.class.where("priority > ?", self.priority_was).update_all("priority = priority - 1")
      else
        nil
      end
      if self.priority > self.priority_was
        self.class.where("priority <= ?", self.priority).update_all("priority = priority - 1")
        self.class.where("priority < ?", self.priority_was).update_all("priority = priority + 1")
      else
        nil
      end
    end
    

    end

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

Sidebar

Related Questions

I have a simple work role in azure that does some data processing on
I have a simple Class Hierarchy that I am trying to get to work
I have two simple submit buttons on my form, which work just fine: <button
I have a simple Poco-Model using abstract classes, and it seems not to work
I have a very simple JavaScript/jquery code which won't just work correctly. The problem
I have a very simple scenario which I cannot get to work. I am
I have written a simple code in page test_cookie.php to work with cookies. if(isset($_GET['data']))
i have the following simple code, but it doesn,t work <ul> <li id=one onmouseover=this.style.background-color='white';>
I'm sure this is very simple. I have gotten colorbox to work before, but
This should be quite simple, but I can't work out the syntax. I have

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.