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的寶石。
添
哇 - 我又碰到了這個:http://stackoverflow.com/questions/27697076/mongoid-association-creating-unwanted-records – bonhoffer