Declarative transaction in Rails #ginzarb

When talking about declarative transactions at Ginza Ruby Conference 01, @a_matsuda doesn't want Rails to have declarative transactions? I was saying something like that, so I made it quickly with gem.

https://github.com/ttanimichi/transactional

What is a declarative transaction?

A declarative transaction is a method of declaratively describing that a transaction is automatically executed when a method is called. Java's Spring Framework is a well-known implementation framework. For Spring Framework, you can use declarative transactions by annotating methods with @Transactional.

@Transactional
public void create(int id) {
    // do something
}

How to use

Add it to your Gemfile and do a bundle install.

gem 'transactional'

All you have to do now is specify in transactional the action you want to make a transaction with in Controller.

class YourController < ApplicationController
+  transactional :create, :update
+
  def index
  end

  def create
    Post.create!(name: 'john', age: 42)
    Topic.create!(title: 'invalid title')
    render plain: :created
  end

  def update
  end
end

Will automatically take a transaction when the specified action is called.

Processing by YourController#create as HTML
   (0.2ms)  BEGIN
  SQL (0.5ms)  INSERT INTO "posts" ("name", "age", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["name", "john"], ["age", 42], ["created_at", "2017-08-06 08:23:13.980493"], ["updated_at", "2017-08-06 08:23:13.980493"]]
   (0.2ms)  ROLLBACK

In this example, you can see that the posts INSERT is rolled back when Topic's create! Throws an exception.

Recommended Posts

Declarative transaction in Rails #ginzarb
Group_by in Rails
Model association in Rails
Adding columns in Rails
Disable turbolinks in Rails
CSRF measures in Rails
^, $ in Rails regular expression
Use images in Rails
Understand migration in rails
Split routes.rb in Rails6
Implement markdown in Rails
Get UserAgent in [Rails] controller
Implement follow function in Rails
Japaneseize using i18n with Rails
Implement LTI authentication in Rails
Error in rails db: migrate
Gem often used in Rails
Display Flash messages in Rails
View monthly calendar in Rails
Implement import process in Rails
Use multiple checkboxes in Rails6!
Rewrite Routes in Rails Engine
Rails: Capture regular expressions in emails!
[Rails] Session timeout setting in devise
Add a search function in Rails.
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
[rails] Login screen implementation in devise
[Rails] How to write in Japanese
[Rails] Unexpected validation error in devise
About the symbol <%%> in Rails erb
[Rails] Use cookies in API mode
Implement simple login function in Rails
Create a new app in Rails
Ruby on Rails Japanese-English support i18n
[Solution] Webpacker :: Manifest :: MissingEntryError in Rails
Implement a contact form in Rails
Remove "assets" and "turbolinks" in "Rails6".
CRUD features and MVC in Rails
How to introduce jQuery in Rails 6
First pagination feature added in rails
Data is not registered in Rails.
[Rails 6] Customize Bootstrap in Rails + Bootstrap 5.0.0-alpha environment
Implement CSV download function in Rails
Ruby methods often used in Rails
How to install Swiper in Rails