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 6697291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:26:36+00:00 2026-05-26T06:26:36+00:00

Model: class Plan include Mongoid::Document # Fields field :name, type: String # Relationships references_many

  • 0

Model:

class Plan
  include Mongoid::Document
  # Fields
  field :name, type: String

  # Relationships
  references_many :sub_plans,
    :autosave           => true,
    :class_name         => 'Plan',
    :inverse_class_name => 'Plan',
    :inverse_of         => :super_plans
  references_many :super_plans,
    :class_name         => 'Plan',
    :inverse_class_name => 'Plan',
    :inverse_of         => :sub_plans
  accepts_nested_attributes_for :sub_plans

  # Validations
  validates_presence_of :name
end

Test:

describe Plan do
  it "should always have a name" do
    plan = Plan.new
    plan.save.should == false
    plan[:name] = "World Domination"
    plan.save.should == true
  end
  it "should allow nested plan creation" do
    plan = Plan.new(:name => "World Domination", :sub_plans_attributes => {
      :name => "Get $50b",
      :sub_plans_attributes => {
        :name => "Invent"
      }
    });
    plan.sub_plans.count.should == 1
    plan.sub_plans.first.name.should == "Get $50b"
    plan.sub_plans.first.sub_plans.count.should == 1
    plan.sub_plans.first.sub_plans.first.name.should == "Invent"
  end
end

Output with -b:

Running spec/models/plan_spec.rb
.F

Failures:

  1) Plan should allow nested plan creation
     Failure/Error: plan = Plan.new(:name => "World Domination", :sub_plans_attributes => {
     TypeError:
       can't convert Symbol into String
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/relations/builders/nested_attributes/many.rb:67:in `delete'
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/relations/builders/nested_attributes/many.rb:67:in `destroyable?'
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/relations/builders/nested_attributes/many.rb:103:in `process'
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/relations/builders/nested_attributes/many.rb:30:in `block in build'
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/relations/builders/nested_attributes/many.rb:26:in `each'
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/relations/builders/nested_attributes/many.rb:26:in `build'
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/nested_attributes.rb:47:in `block (3 levels) in accepts_nested_attributes_for'
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/attributes.rb:193:in `_assigning'
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/nested_attributes.rb:45:in `block (2 levels) in accepts_nested_attributes_for'
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/attributes/processing.rb:111:in `block in process_nested'
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/attributes/processing.rb:110:in `each_pair'
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/attributes/processing.rb:110:in `process_nested'
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/attributes/processing.rb:122:in `process_pending'
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/attributes/processing.rb:29:in `process'
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/document.rb:131:in `block in initialize'
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/relations/builders.rb:47:in `_building'
     # /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/document.rb:127:in `initialize'
     # ./spec/models/plan_spec.rb:11:in `new'
     # ./spec/models/plan_spec.rb:11:in `block (2 levels) in <top (required)>'
     # /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:48:in `instance_eval'
     # /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:48:in `block in run'
         # /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:107:in `with_around_hooks'
     # /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:45:in `run'
     # /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:294:in `block in run_examples'
     # /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:290:in `map'
     # /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:290:in `run_examples'
     # /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:262:in `run'
     # /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `block (2 levels) in run'
     # /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `map'
     # /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `block in run'
     # /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/reporter.rb:12:in `report'
     # /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:21:in `run'
     # /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/monkey/spork/test_framework/rspec.rb:5:in `run_tests'
     # /usr/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc9/lib/spork/run_strategy/forking.rb:13:in `block in run'
     # /usr/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc9/lib/spork/forker.rb:21:in `block in initialize'
     # /usr/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc9/lib/spork/forker.rb:18:in `fork'
     # /usr/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc9/lib/spork/forker.rb:18:in `initialize'
     # /usr/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc9/lib/spork/run_strategy/forking.rb:9:in `new'
     # /usr/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc9/lib/spork/run_strategy/forking.rb:9:in `run'
     # /usr/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc9/lib/spork/server.rb:48:in `run'
     # /usr/lib/ruby/1.9.1/drb/drb.rb:1558:in `perform_without_block'
     # /usr/lib/ruby/1.9.1/drb/drb.rb:1518:in `perform'
     # /usr/lib/ruby/1.9.1/drb/drb.rb:1592:in `block (2 levels) in main_loop'
     # /usr/lib/ruby/1.9.1/drb/drb.rb:1588:in `loop'
     # /usr/lib/ruby/1.9.1/drb/drb.rb:1588:in `block in main_loop'

Finished in 0.00917 seconds
2 examples, 1 failure

Failed examples:

rspec ./spec/models/plan_spec.rb:10 # Plan should allow nested plan creation

Why am I getting this error, and how can I get my many-many self reference working? I’m using Mongoid version 2.3.1 and Rails version 3.1.1. Thanks

  • 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-26T06:26:36+00:00Added an answer on May 26, 2026 at 6:26 am

    After several hours spent trying several different combinations and finally giving up and sleeping on it, I have finally got it to work! Model:

    class Plan
      include Mongoid::Document
      # Fields
      field :name, type: String
    
      # Relationships
      has_and_belongs_to_many :subplans, :inverse_of => :parents,  :class_name => 'Plan', :autosave => true
      has_and_belongs_to_many :parents,  :inverse_of => :subplans, :class_name => 'Plan', :autosave => true
      accepts_nested_attributes_for :subplans
    
      # Validations
      validates_presence_of :name
    end
    

    Tests:

    describe Plan do
      it "should always have a name" do
        plan = Plan.new
        plan.save.should == false
        plan[:name] = "World Domination"
        plan.save.should == true
      end
      it "should have a nested relationship" do
        root   = Plan.create          name: "Parent Plan"
        child1 = root.subplans.create name: 'Child Plan #1'
        child2 = Plan.create          name: "Child Plan #2"
        root.subplans << child2
    
        root.subplans.size.should == 2
        child1.parents.size.should == 1
        child1.parents.first.should == root
        child2.parents.size.should == 1
        child2.parents.first.should == root
      end
      it "should accept nested attributes for subplans" do
        plan = Plan.new :name => "root",
          :subplans_attributes => [{:name => "child"}]
        plan.save; plan.reload
        plan.subplans.size.should == 1
        plan.subplans.first.name.should == "child"
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#model class Promotion(models.Model): name = models.CharField(max_length=200) start_date = models.DateTimeField() end_date = models.DateTimeField() #view def
Say I have two models in my Django project. class Project(models.Model): name = models.CharField(max_length=256)
My model: class Player(models.Model): player_name = models.CharField(max_length=50) player_email = models.CharField(max_length=50) def __unicode__(self): return self.player_name
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage
Assume I have a Model class called Bird and a instance of Bird called
Here's a Django model class I wrote. This class gets a keyerror when I
I have a simple model class (Part), which pulls from it's information from a
Given the Model: class Profile(models.Model): user = models.ForeignKey(User, unique=True) class Thingie(models.Model): children = models.ManyToManyField('self',
How should model class's equals and hashcode be implemented in Hibernate? What are the

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.