Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have a model called "Transaction" in Rails?

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?

like image 959
doremi Avatar asked Dec 09 '14 20:12

doremi


People also ask

Are transactions per model or per database?

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:

What have you learned from Ruby on rails active record transaction?

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.

Why is the transaction class method called on some active record classes?

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.

What is the difference between model instance and transaction?

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:


1 Answers

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.

like image 65
Brian Avatar answered Sep 30 '22 04:09

Brian