active_record-acts_as icon indicating copy to clipboard operation
active_record-acts_as copied to clipboard

Actable model is saved twice

Open allenwq opened this issue 8 years ago • 10 comments

actable.save will trigger two update queries and the after_save callback is called twice.

I think this is because of the bi-directional auto_save: true on actings as and actable model.

allenwq avatar Oct 13 '16 05:10 allenwq

We need both autosaves AFAICS. Happy to remove one if there's an alternative though! :)

manuelmeurer avatar Mar 22 '17 10:03 manuelmeurer

not asking to remove one, but the issue is after_save callbacks are called twice, and duplicated queries are generated, isn't that an issue with the gem ?

allenwq avatar Mar 22 '17 16:03 allenwq

Yeah sure, it might be. If you know how to fix it, please submit a PR!

manuelmeurer avatar Mar 23 '17 07:03 manuelmeurer

@manuelmeurer We have a workaround that uses after_save instead of autosave and that fixed the issue on our project, not sure if you are interested in the way we are doing.

however, I think we should open the issue until someone submits a PR, what you think ? 😄

allenwq avatar Mar 23 '17 12:03 allenwq

Can you post the code of your workaround? I'm happy to try it out to see if it fixes the problem! 😄

manuelmeurer avatar Mar 23 '17 16:03 manuelmeurer

@manuelmeurer Thanks ! 😄

Here's the commit: https://github.com/Coursemology/active_record-acts_as/commit/ca24c95b8f1a1ce2f00fb18cf93ff000358e05e3

We have tested and used in our production product ( 80+ tables/models ) and turns out to be pretty stable.

allenwq avatar Mar 24 '17 02:03 allenwq

Could you test if this is still a problem with the current gem version (2.2.0)? I just tried it out on the console and the after_save callbacks on the supermodel and submodel were only called once each.

manuelmeurer avatar Apr 08 '17 08:04 manuelmeurer

@manuelmeurer Can let me know which rails version that you were testing on ?

allenwq avatar Apr 09 '17 15:04 allenwq

4.2.8

manuelmeurer avatar Apr 09 '17 16:04 manuelmeurer

Let you know first @manuelmeurer, it still happens to us with 2.2.1 in rails 4.2.8.

I will see if can get an example app to reproduce this, but that will take some time...

allenwq avatar Apr 10 '17 07:04 allenwq