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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:13:45+00:00 2026-05-25T12:13:45+00:00

I have a Subject model which represents a treeview with parent children nodes. To

  • 0

I have a Subject model which represents a treeview with parent children nodes.

To move a subject to another branch/node, I need to have a 2 subject id’s that represent from and to values.

I have started by putting all of the logic into the controller but am now wanting to reuse the copy method and would like to set it up in the model.

Here is some of my controller code.

def copy
    from = Subject.find(params[:from])
    to = Subject.find(params[:to])

    if to.is_descendant_of? from
        render :json => {:error => ["Can't move branch because the target is a descendant."]}.to_json,  :status => :bad_request
        return
    end

    if to.read_only?
        render :json => {:error => ["Can't copy to this branch as it is read only."    ]}.to_json,  :status => :bad_request
        return
    end

    if params[:subjects] == 'copy'
      subject = Subject.create(:name => from.name, :description => from.description, :parent_id => to.id)

      #recursively walk the tree
      copy_tree(from, subject)
    else
      #move the tree
      if !(from.read_only or to.read_only)
        to.children << from
      end
    end

end

Here is what I started doing in my model

class Subject < ActiveRecord::Base



    def self.copy(from, to, operations)

        from = Subject.find(from)
        to = Subject.find(to)

        if to.is_descendant_of? from
           #how do I add validation errors on this static method?

        end

    end
end

My first concern is how to add errors to a static method in a model?

I’m not sure if I’m going about it the right way by using a static method or an instance method.

Anyone able to give me a bit of help in refactoring this 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-25T12:13:46+00:00Added an answer on May 25, 2026 at 12:13 pm

    You have three possible solutions. ( I prefer the 3rd approach)

    1) Return nil on success, error string on failure

    # model code
    def self.copy(from, to, operations)
      if  to.is_descendant_of? from
        return "Can't move branch because the target is a descendant."
      end
    end
    
    # controller code
    error = Subject.copy(params[:from], params[:to], ..) 
    if (error)
      return render(:json => {:error => [error]},  :status => :bad_request)
    end
    

    2) Throw exception on error

    def self.copy(from, to, operations)
      if  to.is_descendant_of? from
        throw "Can't move branch because the target is a descendant."
      end
    end
    
    # controller code
    begin
      Subject.copy(params[:from], params[:to], ..) 
    rescue Exception => ex
      return render(:json => {:error => [ex.to_s]},  :status => :bad_request)
    end
    

    3) Add an instance method on Subject class.

    def copy_from(from, operations)
      if  is_descendant_of? from
        errors.add_to_base("Can't move branch because the target is a descendant.")
        return false
      end
    
      return true #upon success
    end
    
    # controller code
    from = Subject.find(params[:from]) #resolve from and to
    to = Subject.find(params[:to])
    
    if to.copy_from(from)
      # success
    else
      # errors
      return render(:json => {:error => [to.errors]},  :status => :bad_request)
    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 contact form with Subject and Message that I want to
I have written an Android application that uses SQLite database which is saved in
I have a Moderator model which basically queries web site related stat results from
I have a CodeIgniter MVC application. I have a model called city that has
For one subject in college I need to use Windows Azure . Notice that
I have Django models as follows: class Subject(models.Model): name = models.CharField(max_length=35, unique=True) class Book(models.Model):
I have a ModelForm field that is based on the following Model: class Phrase(models.Model):
Q.1)write a bean which will have a property called model representing model name of
I have an email subject of the form: =?utf-8?B?T3.....?= The body of the email
I have two tables: subject and student. I'm trying to count the number of

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.