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
Would create method be wrapped in ActiveRecord transaction implicitly, or would I need to wrap it into the transaction block? If I do need to wrap it, would this be the best approach?
Brief answer : You need to explicitly wrap your code in a transaction block. Basically you must use transactions when you want to execute a group of SQL statements, to maintain referential integrity.
Further reading: http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html