【问题标题】:Mongoid self referencing with accepts_nested_attributes_for使用 accept_nested_attributes_for 进行 Mongoid 自引用
【发布时间】:2011-10-15 02:21:16
【问题描述】:

型号:

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

测试:

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

使用 -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

为什么会出现此错误,如何让我的多对多自引用工作?我正在使用 Mongoid 2.3.1 版和 Rails 3.1.1 版。谢谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 mongodb mongoid


    【解决方案1】:

    花了几个小时尝试了几种不同的组合,最后放弃并沉睡了,我终于让它工作了!型号:

    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
    

    测试:

    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
    

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      • 1970-01-01
      相关资源
      最近更新 更多