I have a few question about MongoID and embedded relations in Rails.
In my model I have:
class Board
include Mongoid::Document
attr_accessible :title
field :title, :type => String
#has_and_belongs_to_many :users
embeds_many :items
end
when I call
Board.all
it returns the whole collection, including also :items.
I've read in many articles/forums that using MongoDB embedded relations should be preferred over referenced ones but I have some questions:
Up to now I've noticed that the only advantage in using embedded document is for something like what in SQL are called "joins" but I also see a lot of performaces problem, there are important reason to use embedded relations over referenced relations?
EDIT
As pointed out by Adam C my thoughts are releated to situations like these:
as explained before I will have Boards each one with many Items inside it and using Rails scaffolding it generates methods that retrieve the whole Board document from the database but many times (for example when editing a Board) i want to load the document without the Items part.
Since I will be using mostly JSON calls my idea was to add an optional parameter to the url like "?get_items" to be set to TRUE in case I want also to get items, in other situations I would use Mongoid's:
Model.without
For example let's take the index action:
def index
@boards = Board.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @boards }
end
end
I'll need to get only fields specified in Board Model (in that case only :title) without items so I may use:
def index
@boards = Board.without :items
respond_to do |format|
format.html # index.html.erb
format.json { render json: @boards }
end
end
That my cause some problems?
If you need to retrieve items separately, then you should not embed them.
My rules of thumb:
Top-level domain objects (things that you work with one their own, that don't always appear in the context of their "parent") should get their own collections.
Embed when the related things
a. Don't grow unbounded. That is, in the 1-N relation, N is bounded.
b. Always (or nearly always) appear with their parent.
You can also embed if you can prove to yourself that the performance improvements to be gained by embedding outweigh the costs of the multiple queries required to obtain all objects.
Neither embedding nor relating should be preferred. They should be considered equally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With