Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monkeypatch activerecord relations to work with rails >=5.2.0rc2 #435

Merged
merged 1 commit into from
Mar 28, 2018

Conversation

bbonislawski
Copy link
Contributor

Since this thing rails/rails@b0fc04a got merged into rails, AR raises an exception and don't destroy record since relations seem not destroyed(because they're soft deleted instead of destroyed).

We can monkeypatch ActiveRecord this way, but if someone has better solution, I will be glad.

@BenMorganIO
Copy link
Collaborator

@bbonislawski This is some great code, thanks!

Just one small issue is the .class_eval. This one is a little tricky in its behavior vs .include with a module. Are you able to place this nice code in a module then just do:

module CatchHasOneDeletionException
  def delete(...)
    ...
  end
end

ActiveRecord::Associations::HasOneAssociation.include CatchHasOneDeletionException

Or which ever module name you feel better describes what you're achieving.

@bbonislawski
Copy link
Contributor Author

@BenMorganIO fixed. I had to use prepend instead of include and decided to move it to another file

@BenMorganIO
Copy link
Collaborator

BenMorganIO commented Mar 21, 2018

Looks like JRuby is failing: NoMethodError: undefined method 'new_column'

@bbonislawski
Copy link
Contributor Author

bbonislawski commented Mar 22, 2018

@BenMorganIO there is a problem with sqlite3 jdbc adapter(it's based on activerecord ~> 5.1.0 which doesn't match 5.2.0.rc2) so I don't know if there is any sensible way to fix it before release of rails 5.2.0 .

Any ideas?

@BenMorganIO
Copy link
Collaborator

BenMorganIO commented Mar 22, 2018

Crap. I'm not that into the ruby world ATM to be able to provide meaningful thoughts. Maybe someone else will have some thoughts on this. cc @jhawthorn

Edit: was going to ping him about this, but his profile on the solidus slack says he's vacationing until March 26th. I apologize @bbonislawski you may have to wait for a little for this one.

I think JRuby should remain to have support, but if they aren't going to get things working nicely for the upcoming rails 5.2, then we may have to sink them if they don't swim. I would recommend using your branch for the time being until a new release is cut.

@@ -14,7 +14,7 @@ env:
- RAILS='~> 4.2.0'
- RAILS='~> 5.0.0'
- RAILS='~> 5.1.0'
- RAILS='~> 5.2.0.beta2'
- RAILS='~> 5.2.0.rc2'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bbonislawski

Don't you need to update env: RAILS='~> 5.2.0.beta2' line in allowed_failures section?

Copy link
Contributor Author

@bbonislawski bbonislawski Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch @eitoball . Fixed

@mfazekas
Copy link
Contributor

FWIW here is a gist to reproduce the issue:
https://gist.github.com/mfazekas/1a8778dfea45961ec1333506ca12d837

For now we're using the following hack in our soft deleteable models to work around:

  def destroyed?
    if @in_save
      super
    else
      paranoia_destroyed?
    end
  end

  def save(*args)
    @in_save = true
    begin
      return super(*args)
    ensure
      @in_save = false
    end
  end

@rubiii
Copy link

rubiii commented Mar 28, 2018

Thanks for this patch @bbonislawski
I can confirm, that it fixes the problem for us.

@BenMorganIO BenMorganIO merged commit 82ba504 into rubysherpas:core Mar 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants