Reputation: 81
I am using rails 3.2. Here! I am facing a problem, stats below.
I am having two model classes RequestedTrip and TagTrip. Here! are two other models RequestedTripAgent and Tag on which I operate in before_save callback of RequestedTrip and after_update callback of TagTrip.
There is a case in which I am facing Duplicate entry problem. The case is ...
I am updating RequestedTrip object. There is a before_save callback in which I am using find_or_create_by to find or create RequestedTripAgent object. I also assigning Tags to RequestedTrip object through which another callbacks fire in TagTrip after_update. In TagTrip after_update callback, I also using find_or_create_by of same object of RequestedTripAgent.
Here I am using find_or_create_by but getting Duplicate key index error for RequestedTripAgent. What is wrong with me?
Thanks
Upvotes: 2
Views: 886
Reputation: 6121
find_or_create_by
in before_save
is probably creating the resource and again after the callback it is trying to save the same resource..either move everything to after_save
or replace it with .first_or_initialize
.
Upvotes: 1