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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:34:01+00:00 2026-05-29T19:34:01+00:00

My Code: class ActiveRecord::Base def clone!(options = {}) defaults = {:except => [:updated_at, :cerated_at,

  • 0

My Code:

  class  ActiveRecord::Base  
      def clone!(options = {})
        defaults = {:except => [:updated_at, :cerated_at, :id], :shallow => []}
        options = defaults.merge(options)

        skip_attributes = options[:except] or false #attributes not to clone at all
        shallow_attributes = options[:shallow] or false # non-recursivly cloned attributes
        options[:except] << self.class.to_s.foreign_key # add current class to exceptions to prevent infinite loop

        new_model = self.class.new

        self.attributes.each_pair do |attribute, value|
          skip_attribute = (skip_attributes ? skip_attributes.map{|a| a.to_s}.include?(attribute) : false)
          next if skip_attribute

          shallow_copy = (shallow_attributes ? shallow_attributes.map{|s| s.to_s}.include?(attribute) : false)

          if attribute =~ /_id\z/ and (not shallow_copy)
            # assume reference to a different object
            model_table_name = attribute.gsub(/_id\z/, "")
            model_name = model_table_name.camelize

            referenced_object = model_name.constantize.find(value).clone!(options)
            puts attribute.inspect
            puts referenced_object.inspect
            new_model.send("#{attribute}=", referenced_object[:id])
          else
            new_model.send("#{attribute}=", value) 
          end
        end

        new_model.save!
      end
    end

So, one way to call the method is to do:

b = MyObject.find(432).clone!({:shallow => [:account_id, :user_id, :ext_integration_id, :category_id], :except => [:closed_comment_id]})

The issue is MyObject HAS MANY OtherObjects, so MyObject itsself doesn’t directly reference the OtherObjects, because the OtherObjects have the foreign key to MyObject.

How do I find out what the names of the models are that have this relationship?

  • 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-29T19:34:02+00:00Added an answer on May 29, 2026 at 7:34 pm

    This gem does the trick:

    https://github.com/DerNalia/deep_cloning

    currently has the most features of any of the deep_cloning gems.

    Tested on 1.8.7 with Rails 2.3.8

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

Sidebar

Related Questions

I have the following hypothetical code: class User < ActiveRecord::Base def oauth_consumer @oauth_consumer ||=
Let's say we have the following code: class Post < ActiveRecord::Base def self.fix_published_times where(:published
The following code class Schedule < ActiveRecord::Base def self.get today = TZInfo::Timezone.get('America/New_York').utc_to_local(Time.now.utc).to_date end end
This is my code: class Friend < ActiveRecord::Base belongs_to :user belongs_to :friend, :class_name =>
I have a model defined this way class Lga < ActiveRecord::Base validates_uniqueness_of :code validates_presence_of
Can anyone tell me what's wrong with this code? class Dataset < ActiveRecord::Base has_many
If I have the following code: class User < ActiveRecord::Base has_many :problems attr_accessible :email,
Here is my model: class Why < ActiveRecord::Base belongs_to :story after_save :complete private def
I have the following code: class IncidentTag: def __init__(self,tag): self.tag = tag def equals(self,obj):
When I try this code: class MyStuff: def average(a, b, c): # Get the

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.