hzamani / active_record-acts_as

Simulate multi-table inheritance for activerecord models
MIT License
252 stars 86 forks source link

Actable model is saved twice #82

Open allenwq opened 8 years ago

allenwq commented 8 years ago

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.

manuelmeurer commented 7 years ago

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

allenwq commented 7 years ago

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 ?

manuelmeurer commented 7 years ago

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

allenwq commented 7 years ago

@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 ? 😄

manuelmeurer commented 7 years ago

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

allenwq commented 7 years ago

@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.

manuelmeurer commented 7 years ago

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.

allenwq commented 7 years ago

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

manuelmeurer commented 7 years ago

4.2.8

allenwq commented 7 years ago

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...