在我的rails应用程序中,我基本上有一个"帐户"和"交易"模型。 "交易"模型belongs_to
"帐户"模型两次,一次为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
非常感谢任何见解!
此致
答案 0 :(得分:1)
你应该改变
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