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

fix: using with STI models #15

Merged
merged 3 commits into from Feb 11, 2022
Merged

Conversation

michaelkhabarov
Copy link
Contributor

Hello guys. This PR allows to nest ActiveFormModel forms from AR models with STI column.

I have an error trying to use this gem with STI models like that:

class User < ActiveRecord::Base
end

class Client < User
end

class SignUpForm < Client
  include ActiveFormModel
end

SignUpForm.new

Exception:

ActionController::UnfilteredParameters: unable to convert unpermitted parameters to hash

.../actionpack-6.1.4/lib/action_controller/metal/strong_parameters.rb:316:in `to_h'
.../activerecord-6.1.4/lib/active_record/inheritance.rb:278:in `subclass_from_attributes'
.../activerecord-6.1.4/lib/active_record/inheritance.rb:58:in `new'

That's happened because Rails redefines a .new method in active_record/inheritance.rb to find a subclass from attributes passed to Model.new. So #initialize method is not yet called here and we have unpermitted params.

Permitting params in .new instead of #initialize fixes this issue. Please tell me if you see any drawbacks with this approach.

@mokevnin
Copy link
Member

Looks good! Resolve conflicts pls and I'll merge it

@michaelkhabarov
Copy link
Contributor Author

Done

@mokevnin mokevnin merged commit 52d0cc4 into Hexlet:master Feb 11, 2022
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.

None yet

2 participants