VegaStudios VegaStudios -4 years ago 88
Ruby Question

Rails Copying attributes from User object to new object

In the User model I have an archive! method that is called when a User is destroyed. This action creates a new ArchivedUser in separate table.

The ArchivedUser is successfully created, but the way I am manually setting each value is pretty dirty; if a new column is added to the User table it must be added here as well.

I tried to

the attributes, but got
undefined local variable or method

ArchivedUser.create({ |key, _| ArchivedUser.attribute_names.include? key })


How can I iterate through each attribute in the User table when creating an ArchivedUser with self?

def archive!
if ArchivedUser.create(
company_id: self.company_id,
first_name: self.first_name,
last_name: self.last_name,
encrypted_password: self.encrypted_password,
password_salt: self.password_salt,
session_token: self.session_token,
perishable_token: self.perishable_token,
role: self.role,
score: self.score,
created_at: self.created_at,
updated_at: self.updated_at,
api_key: self.api_key,
device_id: self.device_id,
time_zone: self.time_zone,
device_type: self.device_type,
verified_at: self.verified_at,
verification_key: self.verification_key,
uninstalled: self.uninstalled,
device_details: self.device_details,
is_archived: self.is_archived,
registered_at: self.registered_at,
logged_in_at: self.logged_in_at,
state: self.state,
creation_state: self.creation_state,
language_id: self.language_id,
offer_count: self.offer_count,
expired_device_id: self.expired_device_id,
unique_id: self.unique_id,
best_language_code: self.best_language_code,
offer_id: self.offer_id,
vetted_state: self.vetted_state,
photo_path: self.photo_path

self.is_archived = true = "#{}.archived#{}"
self.encrypted_password = nil
self.password_salt = nil
self.session_token = nil
self.perishable_token = nil
self.device_id = nil
self.verification_key = nil!

self.update_column(:api_key, nil)
UserGroup.delete_all(:user_id =>
# handle the ArchivedUser not being created properly

Thanks for viewing :)

Answer Source

You were on the right track with the first implementation. You just have to use self instead of user.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download