0

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
Rumpleteaser
  • 4,142
  • 6
  • 39
  • 52

1 Answers1

0

Try changing: class: 'Transaction' To: class_name: 'Transaction'

From http://guides.rubyonrails.org/association_basics.html

If the name of the other model cannot be derived from the association name, you can use the :class_name option to supply the model name. For example, if an order belongs to a customer, but the actual name of the model containing customers is Patron, you'd set things up this way:

class Order < ActiveRecord::Base belongs_to :customer, class_name: "Patron" end

Ash
  • 24,276
  • 34
  • 107
  • 152