DmitryTsepelev / store_model

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

Override of accepts_nested_attributes_for breaks app start when connection to db is not available #156

Closed Supernich closed 1 year ago

Supernich commented 1 year ago

Override of accepts_nested_attributes_for was calling for attribute_types method which is taking attribute types from schema if your model is active-record, which will not work when you do not yet have db prepared or your new model is not yet created in database but you already declared everything in your model's class. In some cases it was preventing of 'rails db:create' and 'rails db:migrate'

Override of assign_nested_attributes_for_collection_association was accepting more arguments than it's super method declared in active record, which was breaking one_to_many relations with accept_nested_attributes_for this relation.

Also, plain rails do not support per relation declaration of options for accepts_nested_attributes_for (https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for) e.g. so i don't see the reason why it should be there to potentially break default behaviour.

# Bad
accepts_nested_attributes_for [:supplier, allow_destroy: true], [:service_center, update_only: true]

# Good
accepts_nested_attributes_for :supplier, allow_destroy: true
accepts_nested_attributes_for :service_center, update_only: true

Fix accepts_nested_attributes_for raising error when database is not initialized or table for model doesnt exist

Fix assign_nested_attributes_for_collection_association override breaking ActiveRecord::Base#accepts_nested_attributes_for for non storemodel relations

conradbeach commented 9 months ago

In case it's helpful for anyone else, this change caused an exception in our application.

We were using accepts_nested_attributes_for in the following format and it was working correctly on store_model version 2.1.1: accepts_nested_attributes_for [:categories, { allow_destroy: true }]

After upgrading to store_model 2.1.2, we started seeing this error: super: no superclass method 'accepts_nested_attributes_for' for OurAppClass:Class

Changing the accepts_nested_attributes_for to the following format fixed the issue: accepts_nested_attributes_for :categories, allow_destroy: true

Not a big deal, but I wanted to share in case anyone else runs into it.