FactoryGirl工厂用于多对多自参考模型

时间:2012-06-13 20:31:10

标签: ruby-on-rails-3 factory-bot

我有一个自引用的用户模型:

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

但没有运气。你会怎么做?感谢您的任何意见。

0 个答案:

没有答案