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

  • Home
  • SEARCH
  • 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 6560909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:31:19+00:00 2026-05-25T13:31:19+00:00

Here a tricky issue dealing with nested documents, references and embedded documents. You can

  • 0

Here a tricky issue dealing with nested documents, references and embedded documents.
You can find a zip file containing a sample project illustrating the issue.

I’m Using
Rails v3.0.9
Mongo v1.8.3
Mongoid v2.2.0

Models

  • Conversation
  • Message
  • Text
  • Media
  • Link

  • A Conversation embeds_many Messages (autosave => true)

  • A Message has_many Media (autosave => true)
  • Text inherit from Message (STI)
  • Link inherit from Link (STI)
  • A Conversation accept_nested_attributes_for :messages
  • A Message accept_nested_attributes_for :medias

In rails console, let’s do some testing :

ruby-1.9.2-p180 :001 > c = Conversation.new({:messages_attributes => [{'_type' => 'Text', :content => 'content', :medias_attributes => [{'_type' => 'Link', :url => 'google.com'}]}]})
 => #<Conversation _id: 4e65c7275d156d0129000001, _type: nil, created_at: nil, updated_at: nil, title: nil> 
ruby-1.9.2-p180 :002 > c.messages
 => [#<Message _id: 4e65c7275d156d0129000002, _type: "Text", created_at: nil, updated_at: nil, content: "content">] 
ruby-1.9.2-p180 :003 > c.messages.first.medias
 => [#<Media _id: 4e65c7275d156d0129000003, _type: "Link", created_at: nil, updated_at: nil, message_id: BSON::ObjectId('4e65c7275d156d0129000002'), url: "google.com">] 
ruby-1.9.2-p180 :004 > c.save
 => true 
ruby-1.9.2-p180 :005 > c.reload
 => #<Conversation _id: 4e65c7275d156d0129000001, _type: nil, created_at: 2011-09-06 07:09:33 UTC, updated_at: 2011-09-06 07:09:33 UTC, title: nil> 
ruby-1.9.2-p180 :006 > c.messages
 => [#<Text _id: 4e65c7275d156d0129000002, _type: "Text", created_at: nil, updated_at: nil, content: "content">] 
ruby-1.9.2-p180 :007 > c.messages.first.medias
 => [] #Should not be empty

Now if you change the relations between Conversation <=> Message as follow :

  • A Conversation has_many Messages

It works perfectly.
Some rails console testing also :

ruby-1.9.2-p180 :002 > c = Conversation.new({:messages_attributes => [{'_type' => 'Text', :content => 'content', :medias_attributes => [{'_type' => 'Link', :url => 'google.com'}]}]})
 => #<Conversation _id: 4e65c6bb5d156d011a000004, _type: nil, created_at: nil, updated_at: nil, title: nil> 
ruby-1.9.2-p180 :003 > c.messages
 => [#<Message _id: 4e65c6bb5d156d011a000005, _type: "Text", created_at: nil, updated_at: nil, conversation_id: BSON::ObjectId('4e65c6bb5d156d011a000004'), content: "content">] 
ruby-1.9.2-p180 :004 > c.messages.first.medias
 => [#<Media _id: 4e65c6bb5d156d011a000006, _type: "Link", created_at: nil, updated_at: nil, message_id: BSON::ObjectId('4e65c6bb5d156d011a000005'), url: "google.com">] 
ruby-1.9.2-p180 :005 > c.save
 => true 
ruby-1.9.2-p180 :006 > c.reload
 => #<Conversation _id: 4e65c6bb5d156d011a000004, _type: nil, created_at: 2011-09-06 07:07:54 UTC, updated_at: 2011-09-06 07:07:54 UTC, title: nil> 
ruby-1.9.2-p180 :007 > c.messages
 => [#<Text _id: 4e65c6bb5d156d011a000005, _type: "Text", created_at: 2011-09-06 07:07:54 UTC, updated_at: 2011-09-06 07:07:54 UTC, conversation_id: BSON::ObjectId('4e65c6bb5d156d011a000004'), content: "content">] 
ruby-1.9.2-p180 :008 > c.messages.first.medias
 => [#<Link _id: 4e65c6bb5d156d011a000006, _type: "Link", created_at: 2011-09-06 07:07:54 UTC, updated_at: 2011-09-06 07:07:54 UTC, message_id: BSON::ObjectId('4e65c6bb5d156d011a000005'), url: "google.com">]

Here is the link to the sample zip file : http://d.pr/oMUc

  • 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-05-25T13:31:20+00:00Added an answer on May 25, 2026 at 1:31 pm

    Well, not so easy on this one ^^

    However here is the link to the git issue for mongoid : https://github.com/mongoid/mongoid/issues/1216

    For people who have the same issue !

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

Sidebar

Related Questions

Here's a tricky iPhone problem I've been working on. I have three UIScrollViews on
I'm probably missing out on something fundamental here but it seems rather tricky and
Here is the issue I am having: I have a large query that needs
I'm struggling with a strange file name encoding issue when listing directory contents in
I'm missing a trick here I think and can't believe I've never done this
After debugging a particularly tricky issue in VB.NET involving the order in which instance
I've got a tricky issue sending emails with multiple recipients. It's on a company
here's a description of the issue I'm having: The following script will rotate through
In the spirit of this question by Si here: WiX tricks and tips .
here's the trick. gotta convert 'bout 300 files from xls to csv, wrote some

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.