Merging users


Remember that I said that when you log in, your ghost user has to be merged with the real user? Well, this is the code for doing it:

class User < ActiveRecord::Base
  #...
 
  def merge(user_id)
    if user_id != nil
      user = User.find(user_id)
 
      user.weights.each do |weight|
        weight.user = self
        weight.save
      end
 
      user.destroy
    end
  end
end

The problem with this is that it’s very error prone. You have to make sure that every model that is related to the user model gets properly handled.

Related posts:

  1. Making the weight CRUD work per user
  2. Getting the current user
  3. Profile models
  4. Migrations that change the schema
  5. Creating the weight model and scaffolding

, , , ,

  1. No comments yet.
(will not be published)
CommentLuv Enabled