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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:17:43+00:00 2026-05-17T17:17:43+00:00

While creating new records. I need to create more records for the same model.

  • 0

While creating new records. I need to create more records for the same model.

Example ::

class XYZ < ActiveRecord
 def before_save
   # At this point object is already initialized ..
   # And it's containing values.
   # At this point i want to create 10 more records for the same class.
   # something like this
   XYZ.new(:att1 => value1,:att2 => value2,:att3 => self.att1)
 end
end

How may i handle this type of scenario ?
On which call back i have to create more records for the same model ?

  • 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-17T17:17:43+00:00Added an answer on May 17, 2026 at 5:17 pm

    First, this sounds like bad engineering, try to rethink your model in a way that makes what you need.
    maybe if you need to create 10 models of something, do not use the activerecord hooks, otherwise you might incur in infine loops.

    I would recommend

    class XYZ < ActiveRecord
     def self.create10(original_xyz)
       10.times do
         clone = original_xyz.clone
         clone.save
       end
     end
    end
    

    and where in your controller or wherever you have the need to create 10 more, call:

    new_xyz = XYZ.new(:att1 => value1,:att2 => value2,:att3 => self.att1)
    new_xyz.save
    XYZ.create10(new_xyz)
    

    but if you really need to create 10 more on a hook (like before save), do:

    class XYZ < ActiveRecord
    
     before_save create10
    
     attr_acessor :cloned 
    
     def create10
       return if cloned # this will prevent infinit loooooooooooooooop
       10.times do
         clone = self.clone
         clone.cloned = true
         clone.save
       end
     end
    
    end
    

    I did not run this, so, try it first.

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

Sidebar

Related Questions

Hi I am getting the following erro while creating new Account in CRM 2011.
In Visual Studio 2010, while creating new projects, we may select target version to
I am trying to associate existing records while still being able to add new
Good Day, I'm having few issues with Doctrine entities generator while creating new project
While creating the setup for VB.net application I am getting the following warning: Warning
while creating a thread in java, there is two ways such as Extending threads
While creating JavaScript with ASP.NET MVC I noticed several scope warnings and realized that
While creating a command line tool (Mac OS X) project in XCode, one has
While creating an online shop application using play-1.2.4 ,I ran into some problems with
While creating sharing links for facebook, you can personalize a sharing link by doing

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.