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. Getting the current user
  2. Making the weight CRUD work per user
  3. Creating the weight model and scaffolding
  4. Profile models
  5. I'm going to do an experiment today

About J. Pablo Fernández

http://pupeno.om
This entry was posted in other and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre user="" computer="" escaped="">