DynamoDB Dynomite Has One

Important: These docs are for the outdated Jets 5 versions and below. For the latest Jets docs: docs.rubyonjets.com

Example:

app/models/user.rb

class User < ApplicationItem
  has_one :profile
end

There is typically a corresponding belongs_to as the inverse relationship.

app/models/profile.rb

class Profile < ApplicationItem
  belongs_to :user
end

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.

Creating

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.

Deleting

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.

Disassocating

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