"Type Error: class or module required" for two-to-many relationship
"Type Error: class or module required" for two-to-many relationship
在我的 rails 应用程序中,我基本上有一个 "account" 和一个 "transaction" 模型。 "transaction" 模型 belongs_to
"account" 模型两次,一次是 credited_account
,一次是 debited_account
。它看起来像这样:
class Account < ActiveRecord::Base
has_many :credits, :class_name => "Transaction", :foreign_key => 'credited_account_id'
has_many :debits, :class_name => "Transaction", :foreign_key => 'debited_account_id'
# ... validators and such ... #
end
class Transaction < ActiveRecord::Base
belongs_to :credited_account, :class => "Account"
belongs_to :debited_account, :class => "Account"
end
一切正常,但我运行遇到了一些关于我的规格的问题。
使用 RSpec 和 Factory_Girl,每次我 运行 一个调用事务工厂的规范时,我都会得到一个 TypeError。 Rspec输出如下:
Failure/Error: transaction = build(:transaction)
TypeError:
class or module required
下面是我的spec/factories.rb
FactoryGirl.define do
factory :account do
#... account factory ...#
end
factory :transaction do
association :credited_account, factory: :account
#... other attributes set here ...#
association :debited_account, factory: :account
end
end
非常感谢任何见解!
此致
你应该改变
class Transaction < ActiveRecord::Base
belongs_to :credited_account, :class => "Account"
belongs_to :debited_account, :class => "Account"
end
至
class Transaction < ActiveRecord::Base
belongs_to :credited_account, :class_name => "Account"
belongs_to :debited_account, :class_name => "Account"
end
在我的 rails 应用程序中,我基本上有一个 "account" 和一个 "transaction" 模型。 "transaction" 模型 belongs_to
"account" 模型两次,一次是 credited_account
,一次是 debited_account
。它看起来像这样:
class Account < ActiveRecord::Base
has_many :credits, :class_name => "Transaction", :foreign_key => 'credited_account_id'
has_many :debits, :class_name => "Transaction", :foreign_key => 'debited_account_id'
# ... validators and such ... #
end
class Transaction < ActiveRecord::Base
belongs_to :credited_account, :class => "Account"
belongs_to :debited_account, :class => "Account"
end
一切正常,但我运行遇到了一些关于我的规格的问题。
使用 RSpec 和 Factory_Girl,每次我 运行 一个调用事务工厂的规范时,我都会得到一个 TypeError。 Rspec输出如下:
Failure/Error: transaction = build(:transaction)
TypeError:
class or module required
下面是我的spec/factories.rb
FactoryGirl.define do
factory :account do
#... account factory ...#
end
factory :transaction do
association :credited_account, factory: :account
#... other attributes set here ...#
association :debited_account, factory: :account
end
end
非常感谢任何见解!
此致
你应该改变
class Transaction < ActiveRecord::Base
belongs_to :credited_account, :class => "Account"
belongs_to :debited_account, :class => "Account"
end
至
class Transaction < ActiveRecord::Base
belongs_to :credited_account, :class_name => "Account"
belongs_to :debited_account, :class_name => "Account"
end