Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.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 关于将选项传递给特征的Factory Girl语法的问题_Ruby On Rails_Factory Bot - Fatal编程技术网

Ruby on rails 关于将选项传递给特征的Factory Girl语法的问题

Ruby on rails 关于将选项传递给特征的Factory Girl语法的问题,ruby-on-rails,factory-bot,Ruby On Rails,Factory Bot,我正在接管一个有问答部分的项目。我正在添加一个辛迪加功能,并希望有一个问题有一个关系:辛迪加的问题 事实上,我有一个类似于sq=FactoryGirl.create(:question,:with\u syndication)的API,对于简单的情况,我希望类似于sq=FactoryGirl.create(:question,:with\u syndication(syndicatable\u location\u id:345)),但这不起作用。我如何传递特性的选项/参数?我需要对工厂进行哪些

我正在接管一个有问答部分的项目。我正在添加一个辛迪加功能,并希望有一个问题有一个关系:辛迪加的问题

事实上,我有一个类似于
sq=FactoryGirl.create(:question,:with\u syndication)
的API,对于简单的情况,我希望类似于
sq=FactoryGirl.create(:question,:with\u syndication(syndicatable\u location\u id:345))
,但这不起作用。我如何传递特性的选项/参数?我需要对工厂进行哪些更改

我的工厂目前看起来是这样的:

FactoryGirl.define do
  factory :question, class: Content::Question do
    specialty_id 2
    subject { Faker::Lorem.sentence }
    body { Faker::Lorem.paragraph }
    location_id 24005

    trait :with_syndication do
      after(:create) do |q, options|
        create(:syndicatable_question, question_id: q.id, syndicatable_location_id: q.location_id)
      end
    end
  end
end

您需要在trait中添加
transient

FactoryGirl.define do
  factory :question, class: Content::Question do
    specialty_id 2
    subject { Faker::Lorem.sentence }
    body { Faker::Lorem.paragraph }
    location_id 24005

    transient do
      syndicatable_location_id 24005
    end

    trait :with_syndication do
      after(:create) do |q, options|
        create(:syndicatable_question, question_id: q.id, syndicatable_location_id: options.syndicatable_location_id)
      end
    end
  end
end


FactoryGirl.create(:question, :with_syndication, syndicatable_location_id: 345)
瞬态属性

有没有办法使位置id成为瞬态变量?这种复制非常丑陋,静态和动态属性可以创建为瞬态属性。瞬态属性将在的属性_中被忽略,并且不会在模型上设置,即使该属性存在或您试图覆盖它。()但是,如果需要从
question
分配位置id值,则不需要
transient
块。在(:create)
create(:syndicatable\u question,question\u id:q.id,syndicatable\u location\u id:q.location\u id)
FactoryGirl.create(:question,:with\u syndication)