Has_many通过使用factoryGirl

时间:2013-10-18 11:09:00

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

我有这个型号:

class Application < ActiveRecord::Base
  has_many :admin_user_applications, dependent: :destroy
  has_many :admin_users, through: :admin_user_applications, :source => :user, validate: :false
end

class User < ActiveRecord::Base
  has_many :admin_user_applications, dependent: :destroy
  has_many :administrable_applications, through: :admin_user_applications, :source => :application
end

class UserApplication < ActiveRecord::Base
  belongs_to :user
  belongs_to :application
end

我定义了我的应用工厂:

FactoryGirl.define do
  factory :application, :class => Application do
    sequence(:name) {|n| "application-#{n}" }
    association :owner, :factory => :user
  end
end

但是当我这样做时:

app_3 = FactoryGirl.create(:application, admin_users: [@user])
puts @user.admin_user_applications.inspect  # =>[] 

它返回我[]

我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试

@user.admin_user_applications << FactoryGirl.create(:application)