1
  1) BuildsController has a valid factory
 Failure/Error: FactoryGirl.create(:app).should be_valid
 Mongoid::Errors::MixedRelations:

   Problem:
     Referencing a(n) Build document from the App document via a relational association is not allowed since the Build is embedded.
   Summary:
     In order to properly access a(n) Build from App the reference would need to go through the root document of Build. In a simple case this would require Mongoid to store an extra foreign key for the root, in more complex cases where Build is multiple levels deep a key would need to be stored for each parent up the hierarchy.
   Resolution:
     Consider not embedding Build, or do the key storage and access in a custom manner in the application code.
 # ./spec/controllers/builds_controller_spec.rb:12:in `block (2 levels) in <top (required)>'

build.rb

class Build
 include Mongoid::Document
 include Mongoid::Timestamps

 field :note
 embedded_in :own_app, :class_name => "App", :inverse_of => :last_build
 belongs_to :app
end

app.rb

class App
 include Mongoid::Document
 include Mongoid::Timestamps

 has_many :builds
 embeds_one :last_build, :class_name => "Build", :inverse_of => :own_app
end

仕様/工場/apps.rb

FactoryGirl.define do
  factory :app do |f|
    #fields
    last_build { FactoryGirl.build(:build) }
    builds {|b| [ b = FactoryGirl.build(:build) ] }

  end
end

rsepc を実行すると、コンソールがこの例外をスローします。私の英語は下手なので、コードが教えてくれることを願っています。ありがとう !

4

0 に答える 0