Created by: rwehresmann
Showing unpersisted has_one
associations can be misleading and break the application is some cases.
Let's consider the following example:
class Product < ApplicationRecord
has_one :product_meta_tag
def product_meta_tag
super || build_product_meta_tag
end
end
That opens for a scenario where a product.product_meta_tag
will return a non-persisted instance of a ProductMetaTag. In the _show
partial we're just checking if field.data
(i.e., ProductMetaTag instance) exists to display it, and a non-persisted instance would be displayed. The link displayed in the partial (link_to( field.display_associated_resource, [namespace, field.data]
) would be pointing to the index page of product meta tags, what is not desired. Also, in the absence of an index route, the page would break.
The solution: just check if data is persisted to display it or not.