Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy one table record to another in rails

I have a User model and users table. A user can have many phone numbers so I have a separate Model named Phone. I am using this association for that:

Model

User
     attr_accessible :id, :name, :screenname,:fullname,:phones_attributes
     has_many :phones,:dependent => :destroy

Phone
     attr_accessible :phone
     belongs to :users

Above code works fine. Admin want to copy any user's record into user_temp and phone_temp table as well (I have separate models named UserTemp and PhoneTemp).

How can I do this?

like image 436
Gagan Avatar asked Sep 13 '13 12:09

Gagan


1 Answers

The simplest way would be:

phone_item = Phone.find(x)   # Get the phone item you want to copy
                             # you may have obtained this some other way

PhoneTemp.create(phone_item.attributes) if phone_item

Similarly for the User.

like image 99
lurker Avatar answered Oct 21 '22 11:10

lurker