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

Exception is raised when association uses a composite primary/foreign key #3334

Closed
3 of 11 tasks
filipegiusti opened this issue Oct 11, 2024 · 4 comments · May be fixed by #3335
Closed
3 of 11 tasks

Exception is raised when association uses a composite primary/foreign key #3334

filipegiusti opened this issue Oct 11, 2024 · 4 comments · May be fixed by #3335
Labels

Comments

@filipegiusti
Copy link
Contributor

filipegiusti commented Oct 11, 2024

Describe the bug

foreign_key.to_sym on https://github.com/avo-hq/avo/blob/v3.13.5/lib/avo/fields/belongs_to_field.rb#L197 raises NoMethodError: undefined method to_sym' for an instance of Array` when an association uses a composite primary key/foreign key.

Steps to Reproduce

Have a relationship that uses composite primary/foreign keys.

class Membership
  belongs_to :profile, primary_key: %i[organization_id id], query_constraints: %i[organization_id profile_id]
end

class Profile
  has_one :membership, primary_key: %i[organization_id id], query_constraints: %i[organization_id profile_id]
end

Try to update any attribute on Profile through Avo.

Expected behavior & Actual behavior

Expected: Update works.
Actual: Exception it raised with the following backtrace.

NoMethodError: undefined method `to_sym' for an instance of Array
/webapp/vendor/bundle/ruby/3.3.0/gems/avo-3.13.5/lib/avo/fields/belongs_to_field.rb:197:in `to_permitted_param': undefined method `to_sym' for an instance of Array (NoMethodError)
	from /webapp/vendor/bundle/ruby/3.3.0/gems/avo-3.13.5/app/controllers/avo/base_controller.rb:277:in `map'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/avo-3.13.5/app/controllers/avo/base_controller.rb:277:in `permitted_params'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/avo-3.13.5/app/controllers/avo/base_controller.rb:266:in `model_params'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/avo-3.13.5/app/controllers/avo/application_controller.rb:200:in `fill_record'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:362:in `block in make_lambda'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:179:in `block in call'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/abstract_controller/callbacks.rb:34:in `block (2 levels) in <module:Callbacks>'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:180:in `call'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:559:in `block in invoke_before'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:559:in `each'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:559:in `invoke_before'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:119:in `block in run_callbacks'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/i18n-1.14.6/lib/i18n.rb:353:in `with_locale'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/avo-3.13.5/app/controllers/avo/application_controller.rb:287:in `set_avo_locale'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:130:in `block in run_callbacks'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/marginalia-1.11.1/lib/marginalia.rb:109:in `record_query_comment'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:130:in `block in run_callbacks'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/turbo-rails-2.0.10/lib/turbo-rails.rb:24:in `with_request_id'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/turbo-rails-2.0.10/app/controllers/concerns/turbo/request_id_tracking.rb:10:in `turbo_tracking_request_id'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:130:in `block in run_callbacks'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actiontext-7.2.1/lib/action_text/rendering.rb:25:in `with_renderer'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actiontext-7.2.1/lib/action_text/engine.rb:71:in `block (4 levels) in <class:Engine>'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:130:in `instance_exec'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:130:in `block in run_callbacks'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/sentry-rails-5.21.0/lib/sentry/rails/controller_transaction.rb:32:in `block in sentry_around_action'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/sentry-ruby-5.21.0/lib/sentry/hub.rb:108:in `with_child_span'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/sentry-ruby-5.21.0/lib/sentry-ruby.rb:499:in `with_child_span'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/sentry-rails-5.21.0/lib/sentry/rails/controller_transaction.rb:18:in `sentry_around_action'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:130:in `block in run_callbacks'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:141:in `run_callbacks'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/abstract_controller/callbacks.rb:260:in `process_action'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_controller/metal/rescue.rb:27:in `process_action'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_controller/metal/instrumentation.rb:77:in `block in process_action'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/notifications.rb:210:in `block in instrument'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/notifications/instrumenter.rb:58:in `instrument'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/lib/active_support/notifications.rb:210:in `instrument'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_controller/metal/instrumentation.rb:76:in `process_action'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_controller/metal/params_wrapper.rb:259:in `process_action'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/activerecord-7.2.1/lib/active_record/railties/controller_runtime.rb:39:in `process_action'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/abstract_controller/base.rb:163:in `process'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionview-7.2.1/lib/action_view/rendering.rb:40:in `process'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_controller/metal.rb:252:in `dispatch'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_controller/metal.rb:335:in `dispatch'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_dispatch/routing/route_set.rb:67:in `dispatch'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_dispatch/routing/route_set.rb:50:in `serve'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_dispatch/journey/router.rb:53:in `block in serve'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_dispatch/journey/router.rb:133:in `block in find_routes'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_dispatch/journey/router.rb:126:in `each'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_dispatch/journey/router.rb:126:in `find_routes'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_dispatch/journey/router.rb:34:in `serve'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_dispatch/routing/route_set.rb:896:in `call'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/railties-7.2.1/lib/rails/engine.rb:535:in `call'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/railties-7.2.1/lib/rails/railtie.rb:226:in `public_send'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/railties-7.2.1/lib/rails/railtie.rb:226:in `method_missing'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_dispatch/routing/mapper.rb:33:in `block in <class:Constraints>'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_dispatch/routing/mapper.rb:62:in `serve'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_dispatch/journey/router.rb:53:in `block in serve'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_dispatch/journey/router.rb:133:in `block in find_routes'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_dispatch/journey/router.rb:126:in `each'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_dispatch/journey/router.rb:126:in `find_routes'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_dispatch/journey/router.rb:34:in `serve'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/lib/action_dispatch/routing/route_set.rb:896:in `call'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/rack-3.1.7/lib/rack/static.rb:161:in `call'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/rack-3.1.7/lib/rack/static.rb:161:in `call'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/rack-3.1.7/lib/rack/static.rb:161:in `call'
	from /webapp/vendor/bundle/ruby/3.3.0/gems/ra

System configuration

Avo version: 3.13.5

Rails version: 7.2.1

Ruby version: 3.3.0

License type:

  • Community
  • Pro
  • Advanced

Are you using Avo monkey patches, overriding views or view components?

  • Yes. If so, please post code samples.
  • No

Impact

  • High impact (It makes my app un-usable.)
  • Medium impact (I'm annoyed, but I'll live.)
  • Low impact (It's really a tiny thing that I could live with.)

Urgency

  • High urgency (I can't continue development without it.)
  • Medium urgency (I found a workaround, but I'd love to have it fixed.)
  • Low urgency (It can wait. I just wanted you to know about it.)
@filipegiusti
Copy link
Contributor Author

It can be fixed by changing the offending line for:

foreign_key.is_a?(Array) ? foreign_key.map(&:to_sym) : foreign_key.to_sym                                                                                                                                                       

filipegiusti added a commit to filipegiusti/avo that referenced this issue Oct 11, 2024
Fix avo-hq#3334

Fix `NoMethodError: undefined method `to_sym' for an instance of Array` raised by this line when an association uses a composite primary key/foreign key.
@filipegiusti
Copy link
Contributor Author

I created a PR at #3335 but it's missing tests.

Copy link
Contributor

This issue has been marked as stale because there was no activity for the past 15 days.

@github-actions github-actions bot added the Stale label Oct 27, 2024
Copy link
Contributor

Closing this because there was no activity for the past 15 days. Feel free to reopen if new information pops up ✌️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
1 participant