2013-02-13 63 views
1

在我的Profile類中,與n關聯:fields是有問題的。如果它的存在,我運行以下命令:來自n關係的datamapper中的堆棧溢出

user = User.create 
user.profiles << Profiles.new 
user.save 
Profile.all 

下拋出異常:

SystemStackError: stack level too deep 
    from /Users/jon/.rvm/gems/ruby-1.9.3-p194/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319 

這究竟是爲什麼?有沒有解決方法? 下面是代碼:

require 'rubygems' 
require 'data_mapper' 
require 'dm-timestamps' 
require 'active_support/core_ext' 
DataMapper::Model.raise_on_save_failure = true 
class User 
    include DataMapper::Resource 
    property :id, String, :default => lambda {|r,p| SecureRandom.uuid}, :key=>true 
    property :givenName, String, :length => 255, :default => "" 
    property :surname, String, :length => 255, :default => "" 
    property :isTestUser, Boolean, :default => false 
    has n, :organizations 
    has n, :profiles 

    def default_profile 
     profiles.all(:organization => nil).first 
    end 

end 

class Profile 
    include DataMapper::Resource 
    property :id, Serial 
    belongs_to :organization, :required => false 
    belongs_to :user 
    has n, :fields # if i remove this line then Profile.all doesn't puke, if I have this line then any call to Profile.all fails with stack overflow. 
end 

class Field 
    include DataMapper::Resource 
    property :id, Serial 
    property :fieldType, String, :length => 200, :required => true 
    property :valueType, String, :length => 200, :required => true 
    property :value, String, :length => 500, :required => true 
    belongs_to :profile 
end 

class Organization 
    include DataMapper::Resource 

    property :id, Serial 
    property :name, String 
    timestamps :at 

    belongs_to :user, :required=>false 

end 

回答

1

有n個,:場是造成某種名稱衝突的。將字段更改爲ProfileField和:字段以:profile_fields問題消失。