views:

14

answers:

0

Let's say I have a Model, for example User, and I want to merge two instances of this Model, say merge User2 into User1. Explicitly this is what I mean:

  1. If a field is already filled in User1, it should remain the same

  2. If a field is missing in User1 but is present in User2, it should be copied

  3. If SomeModel BelongsTo User, every instance of SomeModel pointing to User2 should be modified to point to User1

  4. Same if SomeModel HasAndBelongsToMany User

  5. If SomeModel HasMany User, and SomeModel1 Has User2 but no other instance Has User1, it should be modified so that SomeModel1 has User1 instead

  6. If SomeModel HasMany User, SomeModel1 Has User1 and SomeModel2 Has User2... well, I'm not sure here, I guess the only solution is to discard SomeModel2, since User1 can BelongTo only one SomeModel.

Finally User2 should be removed.

Is there a way to automate this? Maybe a Behaviour? If not, I may consider creating it, since I will need it a lot.