We'd like to have a model called "Transaction" that corresponds to our "transactions" table for our database in Rails, but this will collide with existing ActiveRecord transactions functionality.
Aside from coming up with a different name for the Transaction model (which I'd like to not do), is there anything else I can do to support the model?
Transactions are per-database connection, not per-model. As the transaction is the part of ActiveRecord::Base so all models can access transactions using class or instance. For Example, below are all valid snippets:
I have learned a lot by reading the API docs at Ruby on Rails Active Record Transaction . Transactions are protective blocks where SQL statements are only permanent if they can all succeed as one atomic action. The classic example is a transfer between two accounts where you can only have a deposit if the withdrawal succeeded and vice versa.
Though the transaction class method is called on some Active Record class, the objects within the transaction block need not all be instances of that class. This is because transactions are per-database connection, not per-model.
The transaction method is also available as a model instance method. For example, you can also do this: A transaction acts on a single database connection. If you have multiple class-specific databases, the transaction will not protect interaction among them. One workaround is to begin a transaction on each class whose models you alter:
I would say no. I tried to create a model named Transaction
and I got an Active Record error when I tried to do some associations with it.
You tried to define an association named transaction on the model Item, but this will conflict with a method transaction already defined by Active Record. Please choose a different association name.
I landed on this Stack Overflow question wondering the same thing but based on my experience now I wouldn't recommend it – it looks like Transaction
is probably a reserved keyword.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With