-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
-
Labels:None
Mongoid doesn't accept to reference an embedded document so I'm trying to think in ways to denormalizing my data for reaching the same functionality but avoiding this error.
Example:
Class User include Mongoid::Document has_many :comments end Class Places include Mongoid::Document embeds_many :comments end class Comment include Mongoid::Document embedded_in :places belongs_to :user end
This triggers the error: Mongoid::Errors::MixedRelations
So my goal is to be able to ask:
(From an User) What are the comments belongs to that user?
(From a Comment) Who is the user who wrote the comment?
My doubts:
Is it a good practices duplicating the comments?
class Comment include Mongoid::Document embedded_in :user embedded_in :places belongs_to :user end
+ Pros: Avoid the reference (faster)
- Cons:
1. Save comments (in Place and User at the same time)
2. There could be an inconsistency between comments in Place and in User if one of the collections were modified without modifying the other.
Is it a better solution integrating everything into a unique collection?
Class User
include Mongoid::Document
embeds_many :places
end
+ Pros: Avoid the reference (faster)
- Cons:
1. Redundancy (place & comments are repeated in every user who did a comment)
2. Users and Places are not really related between them (we are forcing the relationship)
Other alternative?
What schemes do you use for sceneries like this?