Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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 使用FactoryGirl,如何将子属性的accepts_nested_attributes_插入对象I';我在创造什么?_Ruby On Rails_Factory Bot_Nested Attributes - Fatal编程技术网

Ruby on rails 使用FactoryGirl,如何将子属性的accepts_nested_attributes_插入对象I';我在创造什么?

Ruby on rails 使用FactoryGirl,如何将子属性的accepts_nested_attributes_插入对象I';我在创造什么?,ruby-on-rails,factory-bot,nested-attributes,Ruby On Rails,Factory Bot,Nested Attributes,这里有一个方法,你可以在你的规格内做到这一点 factory :payment_object do company_id 1 user_id 1 delivery_date (Date.today + 1) payment_terms 'CBD' vendor 'Apple' currency 'USD' # item # name "Macbook" # quantity 1 # price 1000 # item # name

这里有一个方法,你可以在你的规格内做到这一点

factory :payment_object do
  company_id 1
  user_id 1
  delivery_date (Date.today + 1)
  payment_terms 'CBD'
  vendor 'Apple'
  currency 'USD'

  # item
    # name "Macbook"
    # quantity 1
    # price 1000
  # item
    # name "Magic Mouse"
    # quantity 1
    # price 65   
end
在(:每个)执行之前
@payment\u object=FactoryGirl.build(:payment\u object)
5.5倍

@payment\u object.items这是不直观的,但工厂女孩文档说明了当您搜索“has\u many”时:

基本上,首先,制造另一个工厂:

before(:each) do
  @payment_object = FactoryGirl.build(:payment_object)
  5.times do 
    @payment_object.items << FactoryGirl.build(:item)
  end
  @payment_object.save!
end
然后在代码中使用它,如下所示:

FactoryGirl.define do
  factory :item do
    name "Macbook"
    quantity 1
    price 1000
    payment_object
  end
end

是否接受\u嵌套的\u属性\u?糟糕的标题
FactoryGirl.define do
  factory :payment_object do
    company_id 1
    user_id 1
    delivery_date (Date.today + 1)
    payment_terms 'CBD'
    vendor 'Apple'
    currency 'USD'

    # This is how you reference the list with your items
    ignore do
      items_count 5  # the number items that should be in the list
    end
    after(:create) do |payment_object, evaluator|
        FactoryGirl.create_list(:item, evaluator.items_count, payment_object: payment_object)
    end


  end