in Education by
I want to make a copy of an ActiveRecord object, changing a single field in the process (in addition to the id). What is the simplest way to accomplish this? I realize I could create a new record, and then iterate over each of the fields copying the data field-by-field - but I figured there must be an easier way to do this. Perhaps something like this: new_record = Record.copy(:id) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
To get a copy, use the dup (or clone for < rails 3.1+) method: #rails >= 3.1 new_record = old_record.dup # rails < 3.1 new_record = old_record.clone Then you can change whichever fields you want. ActiveRecord overrides the built-in Object#clone to give you a new (not saved to the DB) record with an unassigned ID. Note that it does not copy associations, so you'll have to do this manually if you need to. Rails 3.1 clone is a shallow copy, use dup instead...

Related questions

0 votes
    I want to make a copy of an ActiveRecord object, changing a single field in the process (in addition ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    I'm upgrading an application from Rails 3.0 to 3.1 and found the following error showing up in my ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    I have several properties that are dependent on the environment the application is running. For example, there ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    In Ruby on Rails Development (or MVC in general), what quick rule should I follow as to where to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 31, 2022 in Education by JackTerrance
0 votes
    The RoR tutorials posit one model per table for the ORM to work. My DB schema has some 70 tables ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I'm implementing charts using The Ziya Charts Gem. Unfortunately, the documentation isn't really helpful or ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    I'm implementing charts using The Ziya Charts Gem. Unfortunately, the documentation isn't really helpful or ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    On destruction of a restful resource, I want to guarantee a few things before I allow a destroy operation ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    I'm implementing charts using The Ziya Charts Gem. Unfortunately, the documentation isn't really helpful or ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    On destruction of a restful resource, I want to guarantee a few things before I allow a destroy operation ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    On destruction of a restful resource, I want to guarantee a few things before I allow a destroy operation ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    What is the meaning, and where is the Ruby documentation for the syntax of: Array(phrases) which I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    What is the meaning, and where is the Ruby documentation for the syntax of: Array(phrases) which I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    And How can I access Google Task API to get/update the google task list by ruby/RoR? JavaScript ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    looking for a way in rails to allow me to write into 2 different data layers (databases) at the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
...