邀请与组织(many:1)和用户(many:many-through)关联。这在发展中起作用我可以创建一个新的邀请,它会发送一封带有邀请的电子邮件。此邀请的值为person_one_idperson_two_idorganization_id。Person-one和Person-two是用户模型中的用户这在开发中有效,控制台可以正确返回Invitation.first.organizationInvitation.first.person_oneInvitation.first.person_two的记录。
问题是我的邮件测试。尽管从fixtures加载的邀请的值为person_one_idperson_two_idorganization_id,但如果在调试器中调用invitation.organizationinvitation.person_oneinvitation.person_two,它将返回nil
有人知道我的测试代码出了什么问题吗?
邮递员:

def invitation(invitation)
  @invitation = invitation
  @organization = invitation.organization   # This returns nil even though invitation has a value for organization_id
  ...etc...
end

固定装置:
invitation_one:
  person_one: users(:one)                 # Loads this person from users fixtures.
  person_two: users(:archer)
  organization: organizations(:one)       # Loads from organizations fixtures.
  message: I am pleased to invite you1

邮件测试:
test "invitation" do
  @invitation = invitations(:invitation_one)
  mail = InvitationMailer.invitation(@invitation)
  # Produces a "NoMethodError: undefined method `id' for nil:NilClass" error since a.o. invitation.organization is nil.
  ...etc...
end

在加载fixture之后,我在测试文件中添加了调试器调试器对@invitation的响应为:
<Invitation id: 749093126, person_one_id: 41652760, person_two_id: 976142315, organization_id: 118162230, email: nil, message: "I am pleased to invite you1", created_at: "2015-07-05 09:06:23", updated_at: "2015-07-07 12:27:19">

这证明了关联(person_one_idperson_two_idorganization_id)具有价值。然而,调试器用nill响应@invitation.organization,对@invitation.person_one@invitation.person_two也一样。

最佳答案

结果问题出在固定装置上。要为我需要使用的装置创建关联,请执行以下操作:

invitation_one:
  person_one: one
  person_two: archer
  organization: one
  message: I am pleased to invite you1

08-08 07:51