samedi 5 janvier 2019

FactoryGirl: Accesing association data inside traits

I have a payment model, invoice model and a join table invoice_payments for allocating payments to invoices. I am creating my payment factory like this:-

FactoryGirl.define do
  factory :payment do
    customer
    user
    date Date.today
    amount 300
    discount 50
    mode "Cash"

    trait :with_invoice_payments do
      invoice = create(:invoice, customer: customer)
      invoice_payments_attributes do 
        attributes = []
        attributes << attributes_for(:invoice_payment, invoice_id: invoice.id)
      end
    end
  end
end

Now, my invoice should have same customer_id as that of this payment being created. That's why I did invoice = create(:invoice, customer: customer) hoping that this will create invoice with same customer as this payment's customer. But this gives me error wrong number of arguments (given 3, expected 1..2) How can I make invoice so that invoice.customer_id be this payment's associated customer_id in the trait itself?

Aucun commentaire:

Enregistrer un commentaire