ruby on rails - ActiveRecord Callback - How to create a new record based on record just saved? -
I should have an ActiveRecord callback What do I need help?
What I am trying to achieve, each time a special record is saved, ActiveCrak actually saves 2 records in the same table.
Why do I want it to be away from it, it should be good to know that every transaction has been saved in 2 separate but similar records in the same table.
Example:
0) I have a table called "Link" that will store people connected with other people. It looks like:
+ ---- + ----------- + --------------- + - - ----------- + | ID | Person_id Original_word Rcvd_person | + ---- + ----------- + --------------- + ------------- +
1) The web app user will enter the initial person and recipient (who is generating or receiving the link). "Person_id" is empty at this point:
+ ---- + ----------- + ------------ - - + ------------- + | ID | Person_id Original_word Rcvd_person | + ---- + ----------- + --------------- + ------------- + | 1 | | 5 | 10 | + ---- + ----------- + --------------- + ------------- +
2) Before saving the record, I need the value of the "person_id" copy "origin_person":
+ ---- + - - -------- + --------------- + ------------- + | ID | Person_id Original_profile | Rcvd_person | + ---- + ----------- + --------------- + ------------- + 1 | 5 | 5 | 10 ++ ---- + ----------- + --------------- + --------- 3) Finally, Once the record has been saved, a duplicate record has been saved to me, but the value has been copied from "rcvd_person" to "person_id":
/ P>
+ ---- + ----------- + --------------- + ------- ------ + | ID | Person_id Original_word RCVD-Male | + ---- + ----------- + --------------- + ------------- + | 1 | 5 | 5 | 10 | + ---- + ----------- + --------- ------ + ------------- | 2 | 10 | 5 | 10 | + ---- + ----------- + - ------------- + ------------- +
This is what I am trying to do to do this, so if you can help me to understand how this can be achieved which can be appreciated.
I would like to keep all this out of the railway tracks, because I do not want hidden fields and which are not hanging around.
One problem is that I was trying to solve it, it was that it would loop, I wrote some code (which was not working) after a record was saved, a new Record will create, but each saved will trigger a new record that had a new save ....
You can add a flag to your model which can be used to prevent line duplication. . That way when you are saved after_ * called callback and then you create a new object and a further duplication to prevent duplication flag is set to create a new entry
lines: < / p>
def in after_create link: duplicate_link attr_accessible: stop_duplication private def duplicate_link until stop_duplication new_link = Link.new # links attrs here new_link.stop_duplication = true new_link.save end end end set
(Obviously this code has not been tested, I just give the example by giving the example Particular had to want it to be) is being added
, why I would think really hard and I need this type of behavior. I think if you need it then something else can be wrong.
Comments
Post a Comment