2011-10-29 85 views
0

我正在使用Mongoid,Rails和Fabrications,並且完全喪失了這種情況。任何想法都非常感謝,但我知道這很複雜。我只是想製作一個用戶,只有四個加入的組,但我一直在加載八個。從協會創建的重複記錄

這裏是我的代碼

 
@user1 = Fabricate.build(:registered) 
 
@user1.joined_groups << [common_group, 
          cali_group, 
          ca46, 
          Fabricate(:polco_group, {:name => "Gang of 13", :type => :custom})] 

相關的部分。當我運行@user1.joined_groups.size我得到4,但是當我做@user1.joined_groups.map(&:name),我得到8所記載:

 
#<PolcoGroup _id: 1 ... member_ids: [], follower_ids: []> 
#<PolcoGroup _id: 1 ... member_ids: [], follower_ids: []> 
#<PolcoGroup _id: 1 ... member_ids: [], follower_ids: []> 
#<PolcoGroup _id: 1 ... member_ids: [], follower_ids: []> 
#<PolcoGroup _id: 1 ... member_ids: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], follower_ids: [1, 1]> 
#<PolcoGroup _id: 1 ... member_ids: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], follower_ids: [1, 1]> 
#<PolcoGroup _id: 1 ... member_ids: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], follower_ids: [1, 1]> 
#<PolcoGroup _id: 1 ... member_ids: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], follower_ids: [1, 1]> 

(其中我已經用所有的BSON :: ObjectId('4eab3ca5f11aac2701000009')語句替換掉了很多中間代碼。

全套代碼可在這裏找到:https://gist.github.com/1323984

大多數bizzarre只是簡單地調用map可能會導致問題。

 
    puts "just created user with these groups:" 
    puts @user1.joined_groups.map(&:name) 
    puts "then secondly" 
    puts @user1.joined_groups.map(&:name) 

生成此(!):

 
just created user with these groups: 
Dan Cole 
CA 
CA46 
Gang of 13 
then secondly 
Dan Cole 
CA 
CA46 
Gang of 13 
Dan Cole 
CA 
CA46 
Gang of 13 

感謝您的見解!經過反覆的嘗試,我無法想出終端中的一種方式來複制這個,所以我懷疑Fabrication的寶石。

+0

哇 - 我又碰到了這個:http://stackoverflow.com/questions/27697076/mongoid-association-creating-unwanted-records – bonhoffer

回答

1

我想問題可能僅僅是你沒有正確地推組到用戶(更新沒了,我得到這個錯誤的標準mongoid對象,所以我完全責備mongoid)。嘗試使用concat或單獨剷起它們。

@user1.joined_groups.concat([common_group, 
         cali_group, 
         ca46, 
         Fabricate(:polco_group, {:name => "Gang of 13", :type => :custom})])