avatar

Neil Robinson

Ruby on Rails

Picture of a Rails ActionView field_for :tag example

Rails Transaction Do

ActiveRecord Transactions You can commit atomic transactions on an ActiveRecord model by calling class method Model.transaction do. This allows the update controller action to update all associated models in one transaction. I have previously wondered how to make sure you can update all your associated models in one big committed transaction and ensure all models are updated through the controller. Now I learned it is using the transaction do block.

Picture of a Rails ActionView field_for :tag example

Rails Views with multiple assocations

I am learning how to create and handle Rails ActionViews with multiple associations. There are many ways to handle associations in rails and I have not seen a consistent use of one paradigm. It is a challenge to think through which model should be saved first, what associated data may already be in the database that you can use option_from_collection_for_select or the fields_for method, or determine which view to modify to bring in the association.