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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:39:27+00:00 2026-05-17T23:39:27+00:00

I have a categories model made with the fantastic awesome_nested set. I have successfully

  • 0

I have a categories model made with the fantastic awesome_nested set.
I have successfully generated the drag and drop tree and successfully generated the complete hash of this tree using SERIALIZELIST plugin and sent it to the “array” method that I have added to my categories controller. (using jquery and nestedsortables)
The hash from my log looks like so …

Processing CategoriesController#array
(for 127.0.0.1 at 2010-08-19 23:12:18)
[POST] Parameters:
{“ul”=>{“0″=>{“class”=>””,
“id”=>”category_1”,
“children”=>{“0″=>{“class”=>””,
“id”=>”category_4”,
“children”=>{“0″=>{“class”=>””,
“id”=>”category_3”}}}}},
“1”=>{“class”=>””, “id”=>”category_2”,
“children”=>{“0″=>{“class”=>””,
“id”=>”category_5”},
“1”=>{“class”=>””,
“id”=>”category_6”}}}}}

i’m just having trouble with the sort function.

Awesome nested set does provide a few move functions but I can’t seem to get my head around it.

I want to do something like this when the user hits save (btw it does an ajax request and passes the above data correctly)

def array
    newlist = params[:ul]
    newlist.each_with_index do |id, index, children|
      #insert code here for saving the re-ordered array
    end
    render :nothing => true
end

I hope this is enough information and hope someone can answer this question.

Cheers,

Matenia

———– UPDATE AND PROGRESS ———–

Since posting this a few days ago, I mucked around with the logger.info in my dev environment to see what was going on behind the scenes.

I ended up writing 2 functions. One to go through the roots of the array and the other to recursively move the children and childrens children into place. But this ends up with too many database calls (there may be no other way to do it though).

the code looks like so …

  def array
    # fetch the current tree
    @allcategories = Category.all
    # assign the sorted tree to a variable
    newlist = params[:ul]
    # initialize the previous item
    previous = nil
    #loop through each item in the new list (passed via ajax)
    newlist.each_with_index do |array, index|
      # get the category id of the item being moved
      moved_item_id = array[1][:id].split(/category_/)
      # find the object that is being moved (in database)
      @current_category = Category.find_by_id(moved_item_id)
      # if this is the first item being moved, move it to the root.
      unless previous.nil?
        @previous_item = Category.find_by_id(previous)
        @current_category.move_to_right_of(@previous_item)
      else
        @current_category.move_to_root
      end
      # then, if this item has children we need to loop through them
      unless array[1][:children].blank?
        # unless there are no children in the array, send it to the recursive children function
        childstuff(array[1], @current_category)
      end
      # set previous to the last moved item, for the next round
      previous = moved_item_id
    end
    render :nothing => true
  end
  def childstuff(node, category)
    # find the category that has been passed into the function
    @selected_category = Category.find(category)
    for child in node[:children]
      child_id = child[1][:id].split(/category_/)
      child_category = Category.find_by_id(child_id)
      child_category.move_to_child_of(@selected_category)
      #if this child has children -- run recursion on this function
      unless child[1][:children].blank?
        childstuff(child[1], child_category)
      end
    end
  end

I hope someone can shed some light on how to make this more efficient and how to reduce the number of database calls. I have thought about writing other functions, but theyre all going to do the same thing.

For this particular project, I don’t believe there would be more than 100 different categories. It’s not the best way, but it works.

Cheers again,

Matenia


THE FINAL WORKAROUND

I had an issue with the above code where it wasn’t saving the children properly.
Here is my latest attempt, which seems to work well.

def array
    # assign the sorted tree to a variable
    newlist = params[:ul]
    # initialize the previous item
    previous = nil
    #loop through each item in the new list (passed via ajax)
    newlist.each_with_index do |array, index|
      # get the category id of the item being moved
      moved_item_id = array[1][:id].split(/category_/)
      # find the object that is being moved (in database)
      @current_category = Category.find_by_id(moved_item_id)
      # if this is the first item being moved, move it to the root.
      unless previous.nil?
        @previous_item = Category.find_by_id(previous)
        @current_category.move_to_right_of(@previous_item)
      else
        @current_category.move_to_root
      end
      # then, if this item has children we need to loop through them
      unless array[1][:children].blank?
        # NOTE: unless there are no children in the array, send it to the recursive children function
        childstuff(array[1], @current_category)
      end
      # set previous to the last moved item, for the next round
      previous = moved_item_id
    end
    Category.rebuild!
    render :nothing => true
  end
  def childstuff(mynode, category)
   # logger.info "node = #{node} caegory = #{category}"
    #loop through it's children
    for child in mynode[:children]
      # get the child id from each child passed into the node (the array)
      child_id = child[1][:id].split(/category_/)
      #find the matching category in the database
      child_category = Category.find_by_id(child_id)
      #move the child to the selected category
      child_category.move_to_child_of(category)
      # loop through the children if any
      unless child[1][:children].blank?
        # if there are children - run them through the same process
        childstuff(child[1], child_category)
      end
    end
  end

still too many database calls, but I guess that’s the price to pay for wanting this functionality as it needs to re-record each item in the database.

Hope this helps someone else in need.
Feel free to msg me if anyone wants help with this.

AWESOME NESTED SET + JQUERY DRAG AND DROP + SERIALIZELIST PLUGIN ….

Cheers,

Matenia

  • 1 1 Answer
  • 1 View
  • 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-17T23:39:27+00:00Added an answer on May 17, 2026 at 11:39 pm

    see edited question above for final workaround ..I have posted the code on github .. although it may have a few bugs and needs refactoring BADLY!
    JQUERY NESTED SORTABLES – DRAG AND DROP – AWESOME NESTED SET

    UPDATE: Added rails 3 example to repo with slightly cleaner code

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

Sidebar

Related Questions

I have Django model that looks like this: class Categories(models.Model): Model for storing the
i have an app called blog and blog and Post and Categories model. url(r'^(?P<slug>[-\w]+)/$',
I have the following models: class ProjectUser(models.Model): categories = models.ManyToManyField('UserCategory', blank=True, null=True) user_id =
I have Product model and it has many categories with a has_many :through association
In Django, I have two models: class Product(models.Model): name = models.CharField(max_length = 50) categories
im trying to make Categories and Subcategories, im checking this models but i have
I have a blog-like application with stories and categories: class Category(models.Model): ... class Story(models.Model):
I have a standard self referencing table of Categories . In my entity model
I have a a post model and a categories model I want to show
I have a model for categories, which contains a circular foreign key. I dumped

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.