westonganger / protected_attributes_continued

The community continued version of protected_attributes for Rails 5+
MIT License
45 stars 33 forks source link

Remove `_deprecated_scope_block` on AssociationRelation #21

Closed foxlance closed 3 years ago

foxlance commented 3 years ago

This fix addresses the NoMethodError: undefined method _deprecated_scope_block for ActiveRecord::AssociationRelation error one might encounter when using .first_or_create, and possibly .first_or_initialize after a where clause on an association, e.g.

Facility.first.amenities.where(name: 'pool').first_or_create
NoMethodError: undefined method `_deprecated_scope_block' for #<ActiveRecord::AssociationRelation []>

This bug is specific to Rails 6.1.x with protected_attributes_continued v1.7.0. Downgrading to protected_attributes_continued v1.6.0 works if one is not after any specific 1.7.0 features.

This fix is inspired by https://github.com/rails/rails/commit/255a7ef7b09ba96f92c7c8a8d59666314551f454.

westonganger commented 3 years ago

PR that makes the original change in behaviour - https://github.com/rails/rails/pull/29301 was merged on June 28, 2017. Was merged before Rails 5.1.3 but I think was only released for 5.2.0 (https://github.com/rails/rails/issues/33387).

PR which removes the scoping block - https://github.com/rails/rails/pull/33394 was merged on July 19, 2018. Would be included in Rails 5.2.1

I think that lets keep the scoping block for versions 5.1 and below. Can you add this change?

foxlance commented 3 years ago

@westonganger Done! Let me know if you find anything else worth addressing.

westonganger commented 3 years ago

@foxlance you added the scoping back for all Rails versions. We want the scoping block to be removed for Rails 5.2+.

foxlance commented 3 years ago

@westonganger Is the recent commit what you have in mind?

westonganger commented 3 years ago

Merged. Thanks for your contribution.

westonganger commented 3 years ago

v1.8.0 is now released which contains this fix.