2017-03-20 6 views
0

私はユーザーの削除をよりうまく処理する方法についていくつかの方向性を探しています。理想的には、列が無効化される前にユーザデータがコピーされて新しいテーブルに格納されるのがarchive!です。アーカイブする前にユーザーデータを複製するためのモデルと移行をセットアップする最良の方法は何ですか? users_controllerで削除する前にユーザーのデータを新しいテーブルにコピーする

、destroyメソッドは、単純な

def destroy 
    load_user 
    @user.archive! 
    flash[:notice] = 'User deleted' 
    redirect_to action: :index 
    ... 
    end 

アーカイブです!ユーザーモデル内のアクションは、すべてのユーザー列を無効にし、ユーザーの削除時にユーザーの電子メールの末尾に接尾辞を追加します。これを行うには

def archive! 
    self.is_archived = true 

    self.email = "#{self.email}.archived#{Time.now.to_i}" 
    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.save! 

    self.update_column(:api_key, nil) 
    UserGroup.delete_all(:user_id => self.id) 
    end 
+0

理由だけではなく、新しいテーブル( 'ArchivedUsers')を作らないし、既存の列に情報を追加する必要はありません - ちょうど'と呼ばれる新しい列を作ります – dax

+0

@daxユーザーテーブル内の情報を無効にする前に、ユーザーを( 'ArchiveUsers')に移動させる方法を教えてください。 – VegaStudios

答えて

1

一つの方法は、単純に新しいテーブルを作成することです - Usersなど、すべて同じ属性を持つArchivedUsersプラス他のどんな情報あなたは(そのようdeleted_atなど)したいと思います。

次に、あなたのarchive!方法は次のようになります。

def archive! 
    additional_attributes = { 
    deleted_at: Time.now.to_i 
    } 

    if ArchivedUser.create!(self.attributes.merge(additional_attributes)) 
    self.destroy! 
    else 
    # handle the ArchivedUser not being created properly 
    end 
end 
関連する問題