DynamoDB Dynomite Has One



class User < ApplicationItem
  has_one :profile

There is typically a corresponding belongs_to as the inverse relationship.


class Profile < ApplicationItem
  belongs_to :user

Note: For the test data set below, the id are set to friendly names to help explain. Here’s how to create the Test Data Set.

When both regular and inverse relationships are defined, Dynomite will update both “foreign key” fields.


user = User.find("bob")
user.profile = Profile.find("profile-bob")
user.profile.id # profile-bob
# The inverse relationship is created
Profile.find("profile-bob").user.id # bob

This will store the ids in the users.profile_id and profiles.user_id fields as String types.


There’s no delete method on a single association like has_one. This is because the user.profile returns a Profile model instance. If you delete the like so:

user = User.find("bob")
user.profile.delete # you're deleting the Profile!

Instead, you disassociate by assigning the has_one relationship to nil.


user = User.find("bob")
user.profile = nil

This will disassociate both sides of the relationship if both have been set up.

user.profile # nil
profile = Profile.find("profile-bob")
profile.user # nil