You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the upsert operation is protected by an arel_condition which results in no records upserted, the model callbacks still fire producing errors. If no records are upserted, no callbacks should be fired.
Take the below as an example:
classSubscription < ApplicationRecordattr_accessible:last_event_idbelongs_to:userafter_save{user.touch}upsert_keys[:store_id]endsubscription1=Subscription.create({store_id: 10,user_id: 5,last_event_id: 12})# succeeds, after_save fires correctlysubscription2=Subscription.upsert!({store_id: 10,user_id: 5,last_event_id: 11},arel_condition: self.arel_table[:last_event_id].lt(11))# fails, after_save should not be called in this case
In the above example, the first subscription is created and the after_save is called. However, in the second subscription, the upsert results in no records touched because the new event id is less than the older event it. While the upsert SQL command succeeds, the returned subscription has all nil fields and the callback fails.
The text was updated successfully, but these errors were encountered:
Hi, I was looking into this and couldn't find any good seam where we could insert a check. We can introspect the result of the SQL query, so in theory it's possible to know when no record was upserted. But I can't find a nice way of conditionally skipping callbacks based upon this info. Help appreciated!
If the
upsert
operation is protected by anarel_condition
which results in no records upserted, the model callbacks still fire producing errors. If no records are upserted, no callbacks should be fired.Take the below as an example:
In the above example, the first subscription is created and the
after_save
is called. However, in the second subscription, the upsert results in no records touched because the new event id is less than the older event it. While the upsert SQL command succeeds, the returned subscription has all nil fields and the callback fails.The text was updated successfully, but these errors were encountered: