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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:00:24+00:00 2026-05-30T22:00:24+00:00

I am using acts_as_list on my Product model, and trying to make it so

  • 0

I am using acts_as_list on my Product model, and trying to make it so you can manually edit the position. Currently if I edit Product 3 from position 3 to position 1, it won’t adjust the other products

Example: Move Product from Position 3 to Position 1

ID Name Position
1 Product 1
2 Product 2
3 Product 3

Will result to

ID Name Position
1 Product 1
2 Product 2
3 Product 1

But I need it to result to

ID Name Position
1 Product 2
2 Product 3
3 Product 1

I’ve come to the conclusion I should add the code to my products update class. Here is what I have so far:

def update
#To edit acts_as_list manually, just get all items with a position higher 
#than the current item and increment each one.

#Find the product to be changed
@product = Product.find(params[:id])

#Find if there is a product already in the requested position
old_product = Product.find_by_position_and_category_id(params[:position], params[:category_id])



  #If the old product has a position less then the current product, return the products in between minus 1
  if @product.position > old_product.position
    products = Product.where(:product < @product.position, :product > @old_product.position)
    products.each do |product|
      product.position + 1
    end
  #If old product position is greater then current product position, return the products in between and decrement all position
  elsif old_product.position < @product.position
    products = Product.all
    products.each do |product|
      product.position = product.position - 1
    end
 end

In this code i’m trying to grab all the products withing the range of the old product and new product, then increment the position of all of them, but my code isn’t working, It seems like the call old_product = Product.find_by_position_and_category_id(params[:position], params[:category_id]) keeps returning nil, when it should return the product with the position of the old product.

Thanks for any help, and let me know if i’m on the right track or if there is a easier way of doing this.

  • 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-30T22:00:25+00:00Added an answer on May 30, 2026 at 10:00 pm

    acts_as_list provides helper methods to do this sort of thing without needing to add your own code. If you want to move your product from position 3 to position 1 and automatically reorder everything else, do this:

    @product.insert_at(1)
    

    Try that instead of manually changing the position and see if that doesn’t simplify your code.

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

Sidebar

Related Questions

I'm using acts_as_taggable_on in a model, and am trying to implement the auto_complete plugin.
I am trying to detect when particular applications are launched. Currently I am using
I am using the acts_as_list gem but when creating new Object the position should
I'm using acts_as_nested_set on a model which is called node. There are multiple lists
I'm currently using the following gems in my application to provide pagination, search, linkable
Background I'm using the acts_as_list gem in a rails app my posts table has
I am trying to attach files in my RoR app. I am using Rails
How can I prevent users from adding new tags which don't already exist in
I'm using acts_as_taggable_on and tags are associated to a Brand model. Users, via a
I have a small rails app that is using acts_as_list to implement drag and

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.