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

Undefined Method for <ActiveRecord::Associations::CollectionProxy []> #33

Closed
wkirby opened this issue Mar 16, 2020 · 3 comments · Fixed by #34
Closed

Undefined Method for <ActiveRecord::Associations::CollectionProxy []> #33

wkirby opened this issue Mar 16, 2020 · 3 comments · Fixed by #34

Comments

@wkirby
Copy link
Contributor

wkirby commented Mar 16, 2020

I am seeing this issue on Rails 6.0.2.1, Administrate 0.13.0 and the current master branch of NestedHasMany.

My configuration is very simple:

class LabDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    payor_lab_settings: Field::NestedHasMany.with_options(skip: :lab)
  }

  ...

  FORM_ATTRIBUTES = [
    :payor_lab_settings
  ].freeze
end

class PayorLabSettingDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    payor: Field::BelongsToSearch.with_options(class_name: 'Payor'),
  }.freeze
  
  ...

  FORM_ATTRIBUTES = [
    :payor
  ].freeze
end

Here is the relevant section of the stack trace:

PayorLabSetting::ActiveRecord_Associations_CollectionProxy#method_missing
activerecord (6.0.2.1) lib/active_record/relation/delegation.rb, line 108
Administrate::Page::Form#get_attribute_value
administrate (0.13.0) lib/administrate/page/base.rb, line 35
Administrate::Page::Form#attribute_field
administrate (0.13.0) lib/administrate/page/base.rb, line 29
block in Administrate::Page::Form#attributes
administrate (0.13.0) lib/administrate/page/form.rb, line 15
Administrate::Page::Form#attributes
administrate (0.13.0) lib/administrate/page/form.rb, line 14
Administrate::Field::NestedHasMany#nested_fields
() bundle/ruby/2.5.0/bundler/gems/administrate-field-nested_has_many-0145a35e4497/lib/administrate/field/nested_has_many.rb, line 23

The exact same code with Field::HasMany works correctly. All dashboards are working individually.

@wkirby
Copy link
Contributor Author

wkirby commented Mar 16, 2020

Further digging, it looks like this is likely due to a change in Administrate between 0.12.0 and 0.13.0:

In 0.13.0 the relevant code: https://github.com/thoughtbot/administrate/blob/v0.13.0/lib/administrate/page/base.rb#L35

Vs 0.12.0: https://github.com/thoughtbot/administrate/blob/v0.12.0/lib/administrate/page/base.rb#L36

Administrate core stopped rescuing NameError and returning nil. My guess is if I revert back to 0.12.0 in Administrate, the code here was always technically broken, because of an attempt to initialize the Form with a collection, rather than an instance of the associated model, but because of Administrate's rescue, it wasn't blowing up, and instead returned a set of Fields with nil values.

@wkirby
Copy link
Contributor Author

wkirby commented Mar 16, 2020

@nickcharlton I have opened PR #34 which addresses this problem. It also captures the same changes made in #31 and #32 in order to get tests passing against Administrate 0.13.0.

@karensg
Copy link

karensg commented Mar 25, 2020

Confirming that the issue is solved with the PR.

nickcharlton pushed a commit that referenced this issue Mar 26, 2020
This is due to a change in Administrate between 0.12.0 and 0.13.0, where
we stopped rescuing NameError and started returning nil.

Also adds an explicit test for administrate 0.13.0.

Fixes #33.
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 a pull request may close this issue.

2 participants