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

Active Record Fails because of an unexpected arguments in find_or_create_by function #268

Closed
tomerfriedman opened this issue Jan 10, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@tomerfriedman
Copy link

tomerfriedman commented Jan 10, 2023

Could not initialize ActiveRecord because of an unexpected error that is thrown and crashs my app.

Share details about your runtime

RUBY_ENGINE: "ruby"
RAILS: "6.0.4.7"

Getting the following error on "find_or_create_by" function:

wrong number of arguments (given 2, expected 0..1)

Attached picture:

image (16)

@tomerfriedman tomerfriedman added the bug Something isn't working label Jan 10, 2023
@tomerfriedman tomerfriedman changed the title Active Record Fails because of an unexpected arguments Active Record Fails because of an unexpected arguments in find_or_create_by function Jan 10, 2023
@arielvalentin
Copy link
Collaborator

Thanks for reporting this issue. This is an issue with the protected_attributes_extended gem overriding and changing ActiveRecord Persistence create signature.

The original Rails create method only accepts a single attributes hash argument:

https://github.com/rails/rails/blob/main/activerecord/lib/active_record/persistence.rb#L33

However the protected_attributes gem redefines the method signature and changes it to accept multiple hash arguments:

https://github.com/westonganger/protected_attributes_continued/blob/master/lib/active_record/mass_assignment_security/persistence.rb#L41

The OTel Instrumentation preserves the method signature when it adds the tracing functionality

https://github.com/open-telemetry/opentelemetry-ruby-contrib/blob/main/instrumentation/active_record/lib/opentelemetry/instrumentation/active_record/patches/persistence_class_methods.rb#L21

During initialization time, the protected attributes gem is mixed in, which adds the parameter, then the OTel instrumentation is mixed and reverts the signature back.

Then when your code runs... 💣 ArgumentError.

I'd argue here that this isn't a bug with the instrumentation but rather with protected attributes because it should not change ActiveRecords public API.

To get your app working again using OTel I recommend that you disable the AR instrumentation but I think that ultimately this should be something that needs to be fixed in the protected attributes gem.

Disabling AR instrumentation should still give you some useful traces.

HTH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants