I recently encountered an error in Rails where I was trying to connect a
Category model to a
Transaction model. I had a
has_one associtation and encountered the following error:
class Category < ActiveRecord::Base has_one :transaction end
"You tried to define an association named transaction on the model Category, but this will conflict with a method transaction already defined by Active Record. Please choose a different association name."
This is caused by ActiveRecord already using a method named "transactions". To handle this issue, choose an appropriate alternative name for the association and specify the model class and foreign key manually.
has_one :association_name, :foreign_key: "key_name", class_name: "ModelClass"
In my case this was implement as:
class Category < ActiveRecord::Base #has_one :transaction has_one :owner, foreign_key: "category_id", class_name: "Transaction" end