views:

23

answers:

0

Hi together, at the moment I'm doing some tests with MongoMapper.

When using relations, http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails sugests to use cached values where it fits...

I take a stripped down example from there:

class Story
  include MongoMapper::Document

  key :title,     String
  key :url,       String
  key :slug,      String

  # Cached values. (How do get these updated, where user changes his username)
  key :username,      String

  # Note this: ids are of class ObjectId.
  key :user_id,   ObjectId
  timestamps!

  # Relationships.
  belongs_to :user

end

class User
  include MongoMapper::Document

  key :logonname,     String
  key :username,      String
  key :pasword,       String
  #and so on....

end

When I use the model above, MongoMapper creates 2 collections. The Story has the user_id of the User who has created the Story.

My question now...

How to I define these cached values... Is there some kind of MongoMapper-Magic that helps me to tell MongoMapper "take :username from User.username" without writing before_save actions?

What happens If the User changes his username? Whats the best way of doing updating the Stories? Before_save in User-class and find all Stories where the user is referenced and update the specific User-field?

This feels not very comfortable because if my Story-Model changes and i want to ad more cached fields, I have to change before_save - actions in 2 Places. In Story, when creating new Story and in (or other referenced Model) User for updating.

Maybe mongoid provides a solution here?!

Thank you very much!!!