mardi 5 mai 2015

Rails: uninitialized constant Model::RenamedAssociation when using class to specify model in relationship

I have 3 tables, Charges, Transactions and Charges_Transactions.

I had to rename the associations on my models as there is an existing transactions method which was interfering with the association. See this article

class Charge < ActiveRecord::Base
  has_and_belongs_to_many :payment_transactions, join_table: "charges_transactions", foreign_key: "charge_id",
      association_foreign_key: "transaction_id", class: 'Transaction'
end

class Transaction < ActiveRecord::Base
  has_and_belongs_to_many :charges, join_table: "charges_transactions", foreign_key: "transaction_id",
      association_foreign_key: "charge_id"
end

@charge.payment_transactions

Now when I try to access the associated transactions for any charge I get the error:

uninitialized constant Charge::PaymentTransaction

Aucun commentaire:

Enregistrer un commentaire