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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s