我有一个自引用的用户模型:
class User < ActiveRecord::Base
has_and_belongs_to_many :following,
:class_name => "User",
:foreign_key => 'follower_id',
:association_foreign_key => 'following_id',
:join_table => 'followers'
has_and_belongs_to_many :followers,
:class_name => "User",
:foreign_key => 'following_id',
:association_foreign_key => 'follower_id',
:join_table => 'followers'
...
end
我无法想出一个FactoryGirl工厂来测试这种行为。我尝试了一些
的变种factory :user do
...
factory :following do
after(:create) do |user, evaluator|
create_list(:user, evaluator.followers_count, follower: user)
end
end
end
但没有运气。你会怎么做?感谢您的任何意见。