Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8382003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:49:41+00:00 2026-06-09T16:49:41+00:00

I’m slowly getting crazy with this problem. I’m pretty sure it is something trivial

  • 0

I’m slowly getting crazy with this problem. I’m pretty sure it is something trivial and I have misunderstood something as I am just beginning my journey with Mongoid (and Ruby on Rails).

My model is the following:

class Drawing
include Mongoid::Document

    field :image_uid
    image_accessor :image
    field :date_created, type: Time, default: Time.now

    recursively_embeds_many

    embedded_in :user
    embedded_in :painting_template, class_name: 'Painting', inverse_of: :template_drawing
    embedded_in :painting_result, class_name: 'Painting', inverse_of: :result_drawing

The User model “embeds_many” Drawings, the Painting model “embeds_one” template_drawing and result_drawing.

What I have been trying to do for the past couple of hours is to create a new Drawing, attach it to a user and define its parent if it has one. I have been playing around in the console a lot but basically what I was doing was similar to this:

User.first.drawings.last.parent_drawing = User.first.drawings.first.dup

Although the console seems happy and returns the content of User.first.drawings.first, User.first.drawings.last.parent_drawing returns nil…

I have tried to assign them to variables and assign variables etc. But nothing changes. I have tried to create new Drawings and put one as the parent of the other also unsuccessfully.

I came to the conclusion that assigning the parent wouldn’t be possible. So I tried to do the other way round and add a child, but I still don’t get an object with a parent or a child.

Here’s some more code that fails (extracted and shortened from my Rails code):

drawing = Drawing.new({:user => @user})
drawing.parent_drawing = @user.drawings.find(parent_id).dup
drawing.save

Funnily, the drawing itself is saved and listed in user.drawings, but doesn’t have a parent.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:49:43+00:00Added an answer on June 9, 2026 at 4:49 pm

    Taking the advice in the comments I tried to re-think my models from scratch. I reread the doc on polymorphic relationships and made Drawing polymorphic. Still a Drawing should be able to be embedded within another Drawing and recursively embedded led nowhere again.

    With the doc/code of Mongoid about cyclic relationships (http://rdoc.info/github/mongoid/mongoid/Mongoid/Relations/Cyclic/ClassMethods) I suspected it was because the embeddings “recursively_embeds_many” is doing were wrong because they didn’t include the fact that Drawing was polymorphic!

    Given that the embedded_in statement is made by the fact that it is polymorphic, adding

    embeds_one :base_drawing, class_name: "Drawing", as: :drawable, cyclic: true
    

    seemed to have the effect I was looking for. It won’t include “children” as “recursively_embeds_many” would have done, but it’s not necessary in my case.

    I hope this helps the next person having trouble with recursively embedded polymorphic relationships.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.