Ruby on Rails
Tuesday, July 24, 2018
Hello all,
I'm currently working on upgrading our application to Rails 4.1 and am running into the following scenario.
We have an existing model named `Transaction` which conflicts with the ActiveRecord instance method of the same name when we refer to this model in a `belongs_to` association like this:
```
class Transaction < ActiveRecord::Base
end
class Thing < ActiveRecord::Base
belongs_to :transaction
end
```
This will trigger this error: "You tried to define an association named transaction on the model Thing, but this will conflict with a method transaction already defined by Active Record. Please choose a different association name."
As far as I can tell, the `transaction` instance method has existed in ActiveRecord since Rails 3.x but this error was added in 4.1.0.rc1 as shown here: https://github.com/rails/rails/pull/13896
My question is twofold:
1) Is there a convenient way to rename an association like this without having to change the model name, db table name, etc?
2) Why is `ActiveRecord::Base#transaction` available as an instance method in the first place? Seems like this is best implemented as a class method and in fact the instance method version in ActiveRecord just calls the class method version anyway (see here: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/transactions.rb#L299-L302).
The word "Transaction" seems as though it would be a commonly used model name for many Rails applications so I'm just trying to gain an understanding of why this would be taken by ActiveRecord.
Thanks,
Jason
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e2a741ff-0400-46d6-91be-ca72b682fa51%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment