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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:56:49+00:00 2026-05-31T02:56:49+00:00

I would like to 1. Grab an object 1 and object 2 from the

  • 0

I would like to
1. Grab an object 1 and object 2 from the database (both have 2 has_many associations each)
2. Create object 3 which is a clone of object 1 and therefore has no associations yet
2. Duplicate object 2’s associations and add them to object 3’s associations
3. Use the new object 3 for operations in memory
4. Exit the process with no permanent change to the database – object 1 and object 2 still have their original associations when the process exits

What instead is happening

  1. I grab an object 1 and object 2 from the database (both have 2 has_many associations each)
  2. I create object 3 which is a clone of object 1 and therefore has no associations yet
  3. I duplicate object 2’s associations and add them to object 3’s associations
  4. I use the new object 3 for operations in memory
  5. I exit the process HOWEVER there is now a permanent change to the database – object 2 no longer has its associations because their keys have been changed to the (Temporary) object 3 id.

Here is my code. Note that all contains an array with an arbitrary # of objects in it

object1 = all.last.clone #we take the most recently created object
all.each do |instance|
  instance_association1 = (instance.association1).dup
  object1.association1 += instance_association1 #BUG this moves the association
  object1.association1.uniq!
  instance_association2 = (instance.association2).dup
  object1.association2 += instance_association2
  object1.association2.uniq!
end

Note that now when I say:
all.last.association1, I get an empty array.

Help!!

  • 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-31T02:56:51+00:00Added an answer on May 31, 2026 at 2:56 am

    MongoMapper’s associations may be a little overzealous with saving. I want to overhaul it sometime, but it’s not an easy problem.

    The code that’s going to tell you when saves happen is many_documents_proxy.rb. When you do my_association = [...], replace is the method that’s called.

    The only method that doesn’t do any saving is build, so you may be able to build up your temporary object like so:

    tmp = all.last.clone
    all.each do |instance|
      instance.association1.each { |doc| tmp.association1.build(doc.attributes) }
      tmp.association1.uniq!
      # ...
    end
    

    More generally, you can convert all your associated documents to arrays and not worry about saving…

    associations1 = []
    associations2 = []
    all.each do |doc|
      associations1 += doc.associations1.to_a
      associations2 += doc.associations2.to_a
    end
    assocations1.uniq!
    assocations2.uniq!
    

    HOWEVER, watch out! Ruby’s uniq method looks like it uses Ruby’s #hash method to compute equality, which may not give you the results you want in this case. Do a few tests to make sure that my_obj.hash == my_obj2.hash if my_obj == my_obj2. See this discussion for strategies on implementing your own #hash method, if that’s the route you need to go.

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

Sidebar

Related Questions

i've got a simple linq to sql object. I grab it from the database
The use case is that I have a remote (read slow) database from which
I have an ALPHA application which allows you grab an obfuscated font from and
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to have a reference for the pros and cons of using
I want to grab a form object from an external javascript function instead of
I have a NSData object coming back from my server, it varies in its
I have a DataGrid (called TheGrid) that I would like to implement copy and
Background : I have a GridView which gets populated from an SqlDataSource via DataSourceID.
have a Grails domain object that has a custom static function to grab data

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.