I'm still pretty new to rails and this seems pretty basic but I can't find a straight answer.
I have 4 models:
def Order
has_many :payments
def Payment
belongs_to :order
has_one :collection
has_one :dispute
def Collection
belongs_to :payment
def Dispute
belongs_to :payment
The "show" Order page has the order details and a loop for all payments:
<% @order.payments.each do |f| %>
<div class="well">
<p> Payment Date: <%= f.date_created %> </p>
<p> Amount: <%= f.amount %> </p>
....# some other fields
<p> Collection Date: <%= f.collection.date_created %> </p>
<p> Disputed Date: <%= f.dispute.date_created %> </p>
</div>
This all works perfect unless there is any value missing in this loop. Which is a lot since many payments don't have "collections" or "disputes". When any value is missing I get the following error:
NoMethodError in Orders#show
undefined method `whatever value is missing' for nil:NilClass
Ideally the missing field would simply render blank, but I'm not sure why it breaks the whole view. Any help would be greatly appreciated!