avatar

Neil Robinson

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.

Picture of a Database Schema

Rails Associations

In Ruby on Rails, the MVC concept is easy to follow when you have a non-relational type of model. The difficulty with rails is that I find organizing and creating the MVC around multiple model associations. The resources and views of associated models seem linked together and brittle. As if I make a change and it breaks a lot of the default views code or the create and update actions. I fear I am misunderstanding rails. I looked through all the guides and there doesn’t appear to be good explanation for how to deal with the views of complex associations. Even on the web, there seems to be limited discussion on this topic.