Rails: 使用 class 指定关系中的模型时未初始化常量 Model::RenamedAssociation
Rails: uninitialized constant Model::RenamedAssociation when using class to specify model in relationship
我有 3 个表,费用、交易和 Charges_Transactions。
我不得不重命名我模型上的关联,因为现有的交易方法干扰了关联。参见 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
现在,当我尝试访问任何收费的关联交易时,我收到错误消息:
uninitialized constant Charge::PaymentTransaction
尝试更改:
class: 'Transaction'
到:
class_name: 'Transaction'
来自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
我有 3 个表,费用、交易和 Charges_Transactions。
我不得不重命名我模型上的关联,因为现有的交易方法干扰了关联。参见 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
现在,当我尝试访问任何收费的关联交易时,我收到错误消息:
uninitialized constant Charge::PaymentTransaction
尝试更改:
class: 'Transaction'
到:
class_name: 'Transaction'
来自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