Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使用Rails';工厂里有很多使用条件吗?_Ruby On Rails_Ruby On Rails 3_Factory Bot - Fatal编程技术网

Ruby on rails 如何使用Rails';工厂里有很多使用条件吗?

Ruby on rails 如何使用Rails';工厂里有很多使用条件吗?,ruby-on-rails,ruby-on-rails-3,factory-bot,Ruby On Rails,Ruby On Rails 3,Factory Bot,在user.rb中我正在使用: has_many :family, -> { where ["user_type = 'family'"] } has_one :friend, -> { where ["user_type = 'friend'"] } 我想在我的用户工厂的traits下使用一个has\u manyconditions行。我该怎么做 我的工厂是: FactoryGirl.define do factory :user do trait :with_fami

user.rb中
我正在使用:

has_many :family, -> { where ["user_type = 'family'"] }
has_one :friend, -> { where ["user_type = 'friend'"] }
我想在我的用户工厂的
traits
下使用一个
has\u many
conditions行。我该怎么做

我的工厂是:

FactoryGirl.define do
  factory :user do

  trait :with_family_photo do
    after(:create) do |photo|
      photo.portrait_type 'portrait_type'
      photo.family_photos << FactoryGirl.create(:with_family_photo, photo_id: photo.id)
    end
  end

  trait :with_friends_photo do
    after(:create) do |photo|
      photo.portrait_type 'portrait_photo'
    end
  end
end
FactoryGirl.define do
工厂:用户做什么
特点:有家庭照吗
在(:create)do| photo之后|
photo.portrait\u type“portrait\u type”

photo.family_照片感谢@SRack提供您的版本+1感谢@SRack提供您的版本+1