Rails, MongoID, and Inline Relationships

I have a few questions about MongoID and inline links 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 entire collection, including also: elements.

I read in many articles / forums that using MongoDB's built-in links should be preferred over linking, but I have a few questions:

  • How about doing it? every time I want to get a board, I will also extract elements from it: sometimes it can be useful, but in the case when I want only information about the board, and not the elements inside it, I have to create a new method to extract the elements.
  • When I want to update an item, DB will reload the entire document, not just the item I want to extract, right?

So far I have noticed that the only advantage when using an inline document is that it is called β€œjoins” in SQL, but I also see a lot of execution problems, is there an important reason to use inline relationships over referenced relationships?

EDIT

As Adam S noted, my thoughts go into such situations:

as explained earlier, I will have boards, each of which contains many elements inside and using Rails scaffolding, it generates methods that retrieve the entire Board document from the database, but many times (for example, when editing the board). I want to load a document without some elements.

Since I will mainly use JSON calls, my idea was to add an optional parameter to the URL, for example "get_items", which should be set to TRUE if I want to also get elements, in other situations I would use Mongoid's:

 Model.without 

For example, suppose an index action:

  def index @boards = Board.all respond_to do |format| format.html # index.html.erb format.json { render json: @boards } end end 

I will need to get only the fields specified in the Board Model (only in this case: name) without elements, so I can use:

  def index @boards = Board.without :items respond_to do |format| format.html # index.html.erb format.json { render json: @boards } end end 

What is my reason for some problems?

+4
source share
1 answer

If you need to get items separately, then you should not embed them.

My rules of thumb are:

  • Top-level domain objects (things that you work with their own that do not always appear in the context of their "parent") must have their own collections.

  • Paste when related things

    a. Do not grow without limits. That is, the ratio 1-NN is limited.

    b. Always (or almost always) appear with their parent.

  • You can also embed, if you can prove to yourself that the performance improvements obtained during the implementation outweigh the costs of several requests necessary to get all the objects.

Neither embedding nor communication should be preferred. They should be considered equally.

+11
source

Source: https://habr.com/ru/post/1402750/


All Articles