DynamoDB Dynomite Belongs To
Important: These docs are for the outdated Jets 5 versions and below. For the latest Jets docs: docs.rubyonjets.com
Example:
app/models/post.rb
class Post < ApplicationItem
belongs_to :user
end
There is typically a corresponding has_many as the inverse relationship.
app/models/user.rb
class User < ApplicationItem
has_many :posts
end
Creating
user = User.find("bob")
Post.find("post-1").user = user
Post.find("post-2").user = user
This will store the ids in the users.posts_ids
field as a String Set and in the posts.user_id
field as a String.
user.posts_ids # <Set: {"post-1"}>
Post.find("post-1").user_id # "bob"
Post.find("post-2").user_id # "bob"
Deleting
There’s no delete method on a single association like belongs_to
. This is because the post.user
returns a User model instance. If you delete the like so:
post = Post.find("post-1")
post.user.delete # you're deleting the User!
Instead, you disassociate by assigning the belongs_to
relationship to nil
.
Disassocating
Post.find("post-1").user = nil
Post.find("post-2").user = nil
This will disassociate both sides of the relationship if both have been set up.
Post.find("post-1").user # nil
Post.find("post-2").user # nil
User.find("bob").posts_ids # nil