Learn Nested Attributes in Rails

Tram Ho

1. Concepts

Nested attibutes are an Active Record feature that supports creating new records through its parent record. By default, the feature is not enabled, if we want to use it, we must declare in the Model we want to perform. Activate it using the method: accepts_nested_attributes_for

accepts_nested_attributes_for :model_name in the above example will create two new methods: author_attributes=(attributes) and pages_attributes=(attributes)

Option :autosave will automatically be enabled on every link that accepts_nested_attributes_for is used

2. Usage

2.1. 1-1 relationship

=> Only create only 1 time in a 1-1 relationship

=> You can also update avatar sub-records via member, note that parent records must be save before child records can be save

=> If you want to update your avatar without providing :id , you must add the option :update_only

=> By default, only create and update on the linked model, if you want to destroy through the parent record, we need to add the option :alow_destroy , then add _destroy to the attributes hash, with a value of true then we can destroy child records.

The child records will not be destroyed if the parent record has not been saved

2.2. 1-n relationship

=> For each hash without the key :id , a new record will be initialized, unless the hash also contains the key _destroy :true

=> We can also set the :reject_if proc option to ignore any new record hashing functions if they do not meet our requirements. With the above example

=> some other way when used with :reject_if

=> If the hash contains the key :id matches the linked record, the matching record will be update

However, the above applies only if the parent model is also being updated. For example, if you want to create a member named Joe and want to update the posts at the same time, it will cause an ActiveRecord :: RecordNotFound error.

To destroy the child records, we do the same thing with a 1-1 relation

2.3. Commonly used options

  • :allow_destroy Allow to delete the child records, passing _destro y with a value of true will delete the child records. Records are deleted only when the parent record has been successfully saved.
  • :reject_if Allows to specify a Proc or a Symbol that points to a method that checks conditions, both of which return true/false . When false :reject_if not called, the nested attributes will be generated with full value for each field except _destroy , when the condition returns false, the _destroy field will be set to true to destroy the record.
  • :limit (1-n) Allows to specify the maximum number of child records that can be created in the parent record. If the number of child records exceeds the limit limit then raised exception NestedAttributes :: TooManyRecords
  • :update_only (1-1) If you want to update a sub-record without adding :id

3. References

To be continued …

https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

The first article still has many shortcomings, I hope you sympathize, the next article will guide you on how to use Nested Attributes in practice. Thank you for scrolling here😆!

Share the news now

Source : Viblo