On Facebook statues, you can start typing an @ and tag a user in a status. This question is not about the frontend, but rather how to store the data for that feature. What is the best way to achieve this functionality in a generic way for representing any mongodb entity in a string in a dynamic way. The goal being if the entity changes, it representation in the stored string also changes. For example one idea I had was this:
Post: {
_id: "48ajsdlfhsdjfkjsljsd"
name: "Post One",
text: "@user is the best for liking @thing, and @thing"
tags: [user:1234, thing:456, thing:789]
}
So I would load this post, then look at the tags, load the models for each tag type and id, then rewrite the string to be: “Chris is the best for liking StackOverflow, and Mongo”. This seems inefficient, any better ideas?
Your answer works Geoff, but I was looking for something a little more efficient. What I have done is store a document in Mongo like this
And the code to translate the tags is run after loading a message. It basically uses the tag to know which type of model to use, and the id to load it. Then it creates an array of values using the as_tag method of a model, or to_s. This array is then substituted into the original string in order replacing the @ signs. A lot like sprintf. So it translates to “Chris is cool for liking bicycles”
Any way to improve the efficiency here?