Are Rails controller actions implicitly controlled?

Given the following code:

def create @something = Something.new(params[:something]) thing = @something.thing # another model # modification of attributes on both 'something' and 'thing' omitted # do I need to wrap it inside a transaction block? @something.save thing.save end 

Can the create method be enclosed in an ActiveRecord transaction implicitly, or would I need to associate it with a transaction block? If I need to wrap it, is this a better approach?

+6
source share
1 answer

Short answer: you need to explicitly wrap your code in a transaction block. Basically, you should use transactions if you want to execute a group of SQL statements to maintain referential integrity.

 Something.transaction do @something.save thing.save end 

Further reading: http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html

+4
source

Source: https://habr.com/ru/post/919544/


All Articles