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

The Archive Base Latest Questions

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

In Rails/ActiveReocrd is there a way to replace one instance with another such that

  • 0

In Rails/ActiveReocrd is there a way to replace one instance with another such that all the relations/foreign keys get resolved.

I could imagine something like this:

//setup
customer1 = Customer.find(1)
customer2 = Customer.find(2)

//this would be cool
customer1.replace_with(customer2)

supposing customer1 was badly configured and someone had gone and created customer2, not knowing about customer1 it would be nice to be able to quickly set everything to customer 2

So, also this would need to update any foreign keys as well

User belongs_to :customer
Website belongs_to :customer

then any Users/Websites with a foreign key customer_id = 1 would automatically get set to 2 by this ‘replace_with’ method

Does such a thing exist?

[I can imagine a hack involving Customer.reflect_on_all_associations(:has_many) etc]

Cheers,
J

  • 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-25T19:06:03+00:00Added an answer on May 25, 2026 at 7:06 pm

    Something like this could work, although there may be a more proper way:

    Updated: Corrected a few errors in the associations example.

    class MyModel < ActiveRecord::Base
    
      ...
    
      # if needed, force logout / expire session in controller beforehand.
      def replace_with (another_record)
        # handles attributes and belongs_to associations
        attribute_hash = another_record.attributes
        attribute_hash.delete('id')
        self.update_attributes!(attribute_hash)
    
        ### Begin association example, not complete.
    
        # generic way of finding model constants
        find_model_proc = Proc.new{ |x| x.to_s.singularize.camelize.constantize }
        model_constant = find_model_proc.call(self.class.name)
    
        # handle :has_one, :has_many associations
        have_ones = model_constant.reflect_on_all_associations(:has_one).find_all{|i| !i.options.include?(:through)}
        have_manys = model_constant.reflect_on_all_associations(:has_many).find_all{|i| !i.options.include?(:through)}
    
        update_assoc_proc = Proc.new do |assoc, associated_record, id|
          primary_key = assoc.primary_key_name.to_sym
          attribs = associated_record.attributes
          attribs[primary_key] = self.id
          associated_record.update_attributes!(attribs)
        end
    
        have_ones.each do |assoc|
          associated_record = self.send(assoc.name)
          unless associated_record.nil?
            update_assoc_proc.call(assoc, associated_record, self.id)
          end
        end
    
        have_manys.each do |assoc|
          associated_records = self.send(assoc.name)
          associated_records.each do |associated_record|
            update_assoc_proc.call(assoc, associated_record, self.id)
          end
        end
    
        ### End association example, not complete.
    
        # and if desired..
        # do not call :destroy if you have any associations set with :dependents => :destroy
        another_record.destroy
      end
    
      ...
    
    end
    

    I’ve included an example for how you could handle some associations, but overall this can become tricky.

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

Sidebar

Related Questions

Is there a way to drop a validation that was set in Rails plugin
Is there a way to catch all uncatched exceptions in a rails controller, like
Is there a way to run processes from rails script/console in the background? I
How to load & get collection of all active-record models used inside rails app.
Is there a way to do this in Rails: I have an activerecord query
Are there any shortcuts in Rails' ActiveRecord that enables you to search by value
In Rails 3, is there a way to produce an ActiveRecord object from xml
We are development an announcement mechanism that works in such way: - Users are
(using Ruby on Rails and ActiveRecord) In short: Is there some way how a
I'm working on a Rails plugin that includes a way to modify the order

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.