MongoDB mongoid self reference relationship

I am new to mongo / mongoid and I am trying to customize my own link links to a table of my sites.

# sites model has_many :child_sites, :class_name => 'Site' belongs_to :parent, :class_name => 'Site' #controller @event = current_site.child_sites.build(params[:site]) 

current_site is a function that returns the current site object.

I get this error -

undefined `entries' method for #

+6
source share
3 answers

You can try changing the definitions of relationships as follows:

 has_many :child_sites, :class_name => 'Site', :cyclic => true belongs_to :parent_site, :class_name => 'Site', :cyclic => true 

I don’t know exactly what he is doing, but I remember that this was discussed in the google google group. If this does not work, try setting inverse_of to relation macros. In most cases, setting inverse_of does the job correctly.

 has_many :child_sites, :class_name => 'Site', :inverse_of => :parent_site belongs_to :parent_site, :class_name => 'Site', :inverse_of => :child_sites 

About additional requests, yes, there will be additional requests when you want to receive the child_sites of the site or the parent site of the site.

You should consider embedding the child sites in the parent site, but keep in mind that you will lose the ability to request child sites using stand-alone. You always had to refer to any child site as parent_site> child_sites.

Also, consider the 16 MB document size limit, which is difficult to access, but may be possible if there are many child sites for the parent, and if you store template information, for example, html, css, etc. in the document itself.

+13
source

Cyclic was originally implemented for embedded documents ( see entry in user group ). For this to work on mongoid 2.3 or higher, you need to remove the cyclic option:

 has_many :child_sites, :class_name => 'Site' belongs_to :parent_site, :class_name => 'Site' 
+4
source

You cannot use recursively_embeds_many or recursively_embeds_one ? http://mongoid.org/en/mongoid/docs/relations.html#common

+2
source

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


All Articles