1
我有一個User has_many :accounts, through: :roles
和User has_many :owned_accounts, through: :ownerships
,我正在使用STI,其中Ownership < Roles
。我無法爲Ownership
型號和owned_account
關聯編寫一個工作工廠,我的測試失敗。與FactoryGirl創建關聯
class User < ActiveRecord::Base
has_many :roles
has_many :accounts, through: :roles
has_many :ownerships
has_many :owned_accounts, through: :ownerships
end
class Account < ActiveRecord::Base
has_many :roles
has_many :users, through: :roles
end
class Role < ActiveRecord::Base
belongs_to :users
belongs_to :accounts
end
class Ownership < Role
end
我有用戶,帳戶和角色的工作工廠;我不能寫一個擁有工廠和owned_accounts關聯,但是:
FactoryGirl.define do
factory :user do
name "Elmer J. Fudd"
end
factory :account do
name "ACME Corporation"
end
factory :owned_account do
name "ACME Corporation"
end
factory :role do
user
account
end
factory :ownership do
user
owned_account
end
end
我開始用這些測試,但我得到一個未初始化的恆定誤差,所有的測試失敗:
describe Ownership do
let(:user) { FactoryGirl.create(:user) }
let(:account) { FactoryGirl.create(:owned_account) }
before do
@ownership = user.ownerships.build
@ownership.account_id = account.id
end
subject { @ownership }
it { should respond_to(:user_id) }
it { should respond_to(:account_id) }
it { should respond_to(:type) }
end
1) Ownership
Failure/Error: let(:account) { FactoryGirl.create(:owned_account) }
NameError:
uninitialized constant OwnedAccount
# ./spec/models/ownership_spec.rb:17:in `block (2 levels) in <top (required)>'
# ./spec/models/ownership_spec.rb:21:in `block (2 levels) in <top (required)>'