Reputation: 573
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?
Upvotes: 5
Views: 6818
Reputation: 58244
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.
Upvotes: 11
Reputation: 2785
If you have separate model for temp_user then you can so something like this
@user = User.find(params[:id]) # find original object
@temp_user = TempUser.create(@user.attributes)
Upvotes: 1