I need help on what I think should be an ActiveRecord Callback.
What I am trying to achieve is, each time a particular record is saved, ActiveRecord actually saves 2 records in the same table.
I will stay away from why I want this, it should be good enough to know that each transaction needs 2 separate but similar records saved in the same table.
Example:
0) I have a table called "links" which will store people linked to other people. It looks like this:
+----+-----------+---------------+-------------+
| id | person_id | origin_person | rcvd_person |
+----+-----------+---------------+-------------+
1) The web app user will enter in the originating person and the receiving person (who is originating or receiving the link). At this point the "person_id" is blank:
+----+-----------+---------------+-------------+
| id | person_id | origin_person | rcvd_person |
+----+-----------+---------------+-------------+
| 1 | | 5 | 10 |
+----+-----------+---------------+-------------+
2) Before the record saves, I need the value of the "origin_person" copied over to the "person_id column:
+----+-----------+---------------+-------------+
| id | person_id | origin_person | rcvd_person |
+----+-----------+---------------+-------------+
| 1 | 5 | 5 | 10 |
+----+-----------+---------------+-------------+
3) Finally, once the record has been saved, I need a duplicate record saved, but with the "rcvd_person" value copied over to the "person_id":
+----+-----------+---------------+-------------+
| id | person_id | origin_person | rcvd_person |
+----+-----------+---------------+-------------+
| 1 | 5 | 5 | 10 |
+----+-----------+---------------+-------------+
| 2 | 10 | 5 | 10 |
+----+-----------+---------------+-------------+
This is exactly what I am trying to do, so if you can help me with an idea of how this could be achieved that would be appreciated.
I would rather keep all of this out of the Rails View, as I don't want hidden fields and what not hanging around.
One problem I ran into in trying to solve this, was that it would loop. I wrote some code (that didn't work) that would create a new record once a record was saved, but each save would trigger a new record which was a new save which ....