DmitryTsepelev / store_model

Work with JSON-backed attributes as ActiveRecord-ish models
MIT License
1.08k stars 87 forks source link

ArgumentError: wrong number of arguments (given 1, expected 0) in random model #121

Closed fabioxgn closed 2 years ago

fabioxgn commented 2 years ago

I added the gem to our project and created 2 models to use jsonb columns and they work as expected, but when running our test suite I'm getting this error in a random model that has nothing to do with the models that I added the json column to:

Looks like you are patching Active Record here? https://github.com/DmitryTsepelev/store_model/blob/master/lib/store_model/railtie.rb

Why is this loaded to models that does not have json models defined?

ArgumentError:
  wrong number of arguments (given 1, expected 0)
./vendor/bundle/ruby/2.7.0/gems/activemodel-6.1.6/lib/active_model/attribute_methods.rb:254:in `method'
./vendor/bundle/ruby/2.7.0/gems/store_model-0.13.0/lib/store_model/ext/parent_assignment.rb:12:in `assign_parent_to_store_model_relation'
./vendor/bundle/ruby/2.7.0/gems/store_model-0.13.0/lib/store_model/ext/active_record/base.rb:18:in `block in _write_attribute'
./vendor/bundle/ruby/2.7.0/gems/store_model-0.13.0/lib/store_model/ext/active_record/base.rb:17:in `tap'
./vendor/bundle/ruby/2.7.0/gems/store_model-0.13.0/lib/store_model/ext/active_record/base.rb:17:in `_write_attribute'
./vendor/bundle/ruby/2.7.0/gems/activemodel-6.1.6/lib/active_model/attribute_methods.rb:254:in `parameters='
./vendor/bundle/ruby/2.7.0/gems/activemodel-6.1.6/lib/active_model/attribute_assignment.rb:49:in `public_send'
./vendor/bundle/ruby/2.7.0/gems/activemodel-6.1.6/lib/active_model/attribute_assignment.rb:49:in `_assign_attribute'
./vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.6/lib/active_record/attribute_assignment.rb:21:in `block in _assign_attributes'
./vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.6/lib/active_record/attribute_assignment.rb:13:in `each'
./vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.6/lib/active_record/attribute_assignment.rb:13:in `_assign_attributes'
./vendor/bundle/ruby/2.7.0/gems/activemodel-6.1.6/lib/active_model/attribute_assignment.rb:34:in `assign_attributes'
DmitryTsepelev commented 2 years ago

Hi! Could you please provide the example of your models, or (even better) a failing spec? The error above is related to the parent tracking feature.

Why is this loaded to models that does not have json models defined?

Because this is how ActiveRecord plugins usually work.

fabioxgn commented 2 years ago

Hi @DmitryTsepelev I think I know why this is happening. It is falling in one specific model, and this model has an attribute called method, and the code is breaking here: https://github.com/DmitryTsepelev/store_model/blob/26b3ac0cc748e3d6378ef9e1e97b729330faa49d/lib/store_model/ext/parent_assignment.rb#L12

So it is calling the method attribute of the model instead of the method ruby function. I'll try to write a failing spec for this, but probably changing this to the following will fix the issue:

attribute.each do { |a| assign_parent_to_singular_store_model(a) }
DmitryTsepelev commented 2 years ago

Closing the issue for now, explanation is here