Factory Girl:如何使用has_one_through关系创建记录?

时间:2017-03-01 04:30:33

标签: ruby-on-rails ruby-on-rails-4 rspec factory-bot

我有如下关系

class LoanApplication < ActiveRecord::Base
  has_one :case_status, :dependent => :destroy
  has_many :case_status_bankers, :class_name => 'CaseStatus', :foreign_key => "loan_application_id"
  has_many :banker_assignments, :through => :case_status_bankers, :source => :banker_assignments, :dependent => :destroy
end

class CaseStatus < ActiveRecord::Base
  belongs_to :loan_application
  has_many :banker_assignments, :dependent => :destroy
end

class BankerAssignment < ActiveRecord::Base
  belongs_to :case_status
  has_one :loan_application, :through => :case_status, :autosave => false
end

如您所见,它显示了3个模型中的不同关系。书面工厂如下

FactoryGirl.define do
  factory :loan_application do
    association :case_status

    factory :mortgage_loan do
      product { build_stubbed(:MLF) }
    end
  end
end

FactoryGirl.define do
  factory :case_status do
  end
end

FactoryGirl.define do
  factory :banker_assignment do
    association :case_status
    association :loan_application
  end
end

当我编写以下规范来创建BankerAssignment记录时,loan_application_id字段始终为零,并且没有关联的loan_application记录。

这是我的规范代码

@mortgage_loan = create(:mortgage_loan, product: create(:MLF))
case_status = create(:case_status, loan_application: @mortgage_loan)
@assignment_status = find_or_create(:bank_assignment_status_pending_doc)
@banker_assignment = create(:banker_assignment, banker: @banker, banker_assignment_status: @assignment_status, loan_application: @mortgage_loan)

但它似乎没有用。

任何人都知道如何编写规范代码,以便在创建银行家分配记录时,它具有关联的loan_application。

由于

0 个答案:

没有答案