A User has many Posts and a Post belongs_to a User. Both have a location. I’d like to store the location for each Post and update the User location with the most recent Post location. Is after_save the way to go? I’m very inexperienced with Mongo DB.
Post Model
class Post
include Mongoid::Document
belongs_to :user
after_save :update_user_location
field :location, :type => String
def update_user_location
#update user's location
end
end
User Model
class User
include Mongoid::Document
has_many :posts
field :location, :type => String
end
(Please don’t say to just get the most recent post location, I need to store it separately for a reason…thx)
This should do it, have you tried anything ? Btw This is really a Mongoid specific question, not really related to MongoDB itself.