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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:41:01+00:00 2026-05-11T23:41:01+00:00

I am creating a Category model and using the awesome_nested_set plugin (a replacement for

  • 0

I am creating a Category model and using the awesome_nested_set plugin (a replacement for acts_as_nested_set) to deal with the hierarchy. With awesome_nested_set, objects are created, then saved, and then placed within the set. As well, lft, rgt and parent_id are attr_protected so they cannot be written to directly.

I am running into two situations when placing the node into the set that I want to be able to catch so that I notify the user (there might be more that I haven’t thought of yet):

  1. A node is attempted to be placed as its own child (self.id == self.parent_id)
  2. A node is attempted to be moved beneath its own descendant (self.descendants.include? self.parent_id == true)

In both cases, the move will fail, but awesome_nested_set will only raise an ActiveRecord::ActiveRecordError exception, with a message that is not as descriptive as I’d like to be able to give the user.

awesome_nested_set has a number of node moving methods, which all call move_to(target, position) (where position is one of :root, :child, :left or :right and target is the related node for all positions but :root). The method fires a before_move callback, but doesn’t provide a way that I can see to validate a move before it happens. In order to validate a move, I’d need access to the target and position, which the callback does not receive.

Does anyone know of either a way to validate a move in awesome_nested_set (either by having a way to pass target and position to the before_move callback of by another method), or another nested set plugin that will let me validate? I’d prefer not to fork or write my own plugin.

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

    Here is the solution I came up with:

    class Category < ActiveRecord::Base
      acts_as_nested_set :dependent => :destroy
    
      #=== Nested set methods ===
    
      def save_with_place_in_set(parent_id = nil)
        Category.transaction do
          return false if !save_without_place_in_set
          raise ActiveRecord::Rollback if !validate_move parent_id
    
          place_in_nested_set parent_id
          return true
        end
    
        return false
      end
    
      alias_method_chain :save, :place_in_set
    
      def validate_move(parent_id)
        raise ActiveRecord::RecordNotSaved, "record must be saved before moved into the nested set" if new_record?
        return true if parent_id.nil?
    
        parent_id = parent_id.to_i
    
        if self.id == parent_id
          @error = :cannot_be_child_of_self
        elsif !Category.all.map(&:id).include?(parent_id)
          @error = :given_parent_is_invalid
        elsif descendants.map(&:id).include? parent_id
          @error = :cannot_be_child_of_descendant
        end
    
        errors.add(:parent_id, @error) if @error
        return @error.nil?
      end
    
      def place_in_nested_set(parent_id)
        if parent_id.nil? || parent_id.blank?
          move_to_root
        else
          move_to_child_of parent_id
        end
        return true
      end
    end
    

    Now, in the controller, I just need to say @category.save(parent_id), where parent_id is nil or the ID of the parent, and the validation, node placement, and save is handled in the model.

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

Sidebar

Ask A Question

Stats

  • Questions 388k
  • Answers 388k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have settled on Block formatting. It bothered my sense… May 15, 2026 at 12:23 am
  • Editorial Team
    Editorial Team added an answer Here is how you can do it: string stringToCheck =… May 15, 2026 at 12:23 am
  • Editorial Team
    Editorial Team added an answer Elegance is a subjective term, I guess there are a… May 15, 2026 at 12:23 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.