我有三个班级:
class User
has_and_belongs_to_many :accounts
end
class Account
has_and_belongs_to_many :users
end
class Idea
belongs_to :user
belongs_to :account
end
这些工厂:
FactoryGirl.define do
sequence :subdomain do |n|
"subdomain#{n}"
end
factory :account do
subdomain { FactoryGirl.generate(:subdomain) }
end
end
FactoryGirl.define do
sequence :email do |n|
"foo-#{n}@example.com"
end
sequence :name do |n|
"Test User #{n}"
end
factory :user do
name { FactoryGirl.generate(:name) }
email { FactoryGirl.generate(:email) }
password "secret"
after_create {|i| Factory(:account, :user_ids => [i.id])}
end
end
Factory.define :idea do |idea|
idea.title 'Lorem ipsum dolor'
idea.description 'Lorem ipsum dolor sit amet'
idea.sequence(:user_id) { |n| Factory(:user).id }
idea.sequence(:account_id) { |n| idea.user.accounts.first.id }
end
最后一个不起作用,并抛出以下错误:
Failure/Error: idea = Factory :idea
NoMethodError:
undefined method `accounts' for #<FactoryGirl::Declaration::Implicit:0x007fcc6c370a60>
如何计算关联用户的Idea和(其中一个)帐户之间的关联?