Currently has_one
always raises an error if the key isn't passed
in as data. This change allows a default to be specified that will
be used when initializing the associated structural class.
One use-case for this is to allow the creation of naive structural
classes with nested associations.
class CustomerDetails
field :account_name, default: nil
has_one :credentials, default: {}, type: Credentials
has_one :billing_address, default: {}, type: BillingAddress
def name
account_name || credentials.name || billing_address.name
end
end
In the above snippet, we know that there are three ways to pass the
customer's name: as top-level account_name
, as name
nested in
credentials
or as name
nested in billing_address
.
Having a default allows you to specify a single access point that
attempts to access the data from the various sources.