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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:36:24+00:00 2026-05-29T15:36:24+00:00

In my code I have a Category model which has many subcategories, and the

  • 0

In my code I have a Category model which has many subcategories, and the Subcategory belongs to a Category.

Subcategory has a :position field to keep track of what position it should line up under the category. Here is my Model code for SubCategory.

class Subcategory < ActiveRecord::Base
  belongs_to :category
  has_many :products
  validates_uniqueness_of :position
  scope :position, order("position asc")
end

As you can see it doesn’t allow for duplicate positions. When it detects that the Subcategory position already exists, I want it to prompt the user to say something like “That position is already taken, but it can still be inserted. Would you like to continue?” Yes | No.

If Yes, Then I need it to add +1 to all the current Category subcategories :position (after the ‘subcategory in question’ position. So if Category 1 has subcategories 1, 2, and 3 when someone tries to add a new subcategory with position 2, I need to drop the current 2 and 3 to 3 and 4 and insert the new subcategory to position 2.

If No, Then I just want it to return to the edit screen.

I am not sure how to code this exactly, I’m assuming the check for :position conflict will be in the subcategories controller, if the save fails.

So my 2 questions are: Where is the best place to put the check for this code, and I need help/ideas writing the code.

  • 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-29T15:36:25+00:00Added an answer on May 29, 2026 at 3:36 pm

    Setting the uniqueness validation aside, this will re-index the positions:

    # build a new subcategory
    subcategory = category.subcategories.build(:foo => :bar)
    
    # some arbitrary, zero-based position desired by your user
    position = 3
    
    # insert the subcategory at the desired position
    category.subcategories.insert(position, subcategory)
    
    # use each with index to re-index all of your subcategories
    category.subcategories.each_with_index do |sc, index|
       sc.position = index
    end
    

    Instead of validating uniqueness you could add a before_validation callback to Category that simply ensures that you have a sorted, contiguous set of positions in subcategories.

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

Sidebar

Related Questions

I have a model class 'Market' which has many products: class Market < ActiveRecord::Base
I have a Category model, and a Project model, which contains a ForeignKey to
I have got a category - model which i am using it for my
I have some Javascript code that creates 2 arrays: One for Product Category and
I have the following code: $(document).ready(function() { // Manage sidebar category display jQuery(#categories >
I have this piece of code in javascript: var catId = $.getURLParam(category); In my
I have 2 models in my Django code: class ModelA(models.Model): name = models.CharField(max_length=255) description
I have hierarchical data stored in the datastore using a model which looks like
I have the following code: ViewData.Model = from m in dataModel.Items where m.Loaned ==
Hi I got a model Category and another model Products. The have the relationship

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.