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

We would like to have a model called "Transaction" that matches our transaction table for our database in Rails, but this will clash with existing ActiveRecord transactions.

Apart from appearing a different name for the transaction model (which I wouldn't want to do), is there anything else I can do to support the model?

+3


source to share


3 answers


I would say no. I tried to create a named model Transaction

and I got an Active Record error when I tried to make some associations with it.

You tried to define an association named transaction on the Item model, but this would conflict with an already defined method transaction on the active record. Choose a different name for the association.



I landed on this question wondering the same thing, but based on my experience, now I wouldn't recommend it - it looks like Transaction

- it's probably a reserved keyword.

0


source


I can't say for sure, but I think everything should be fine. Typically, Rails is pretty proactive in telling you when you're trying to use a name that will conflict, so why not give it a try and see what happens?



If the table name (as opposed to the model name) turns out to be a problem, you can always use self.table_name = "some_other_table_name"

in your model (as described in Active Record Basics ) to give it a different table name.

0


source


Yes, I can use that name for the model and it works fine.

If you're worried about transactions

on Rails, these are the methods for the ActiveRecord :: Transactions class.

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

0


source







All Articles