从关联创建的重复记录 [英] Duplicate Records created from Association

查看:64
本文介绍了从关联创建的重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mongoid,Rails和Fabrications,但完全不知所措.任何想法都很感激,但是我知道这很复杂.我只想伪造一个用户,只有四个加入的组,但我一直要加载八个.

I am using Mongoid, Rails and Fabrications and at a total loss with how this is happening. Any thoughts very appreciated, but I know this pretty complicated. I just want to fabricate a user and have only four joined groups, but I keep getting eight loaded.

这是我代码的相关部分


@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条记录:

When I run @user1.joined_groups.size I get 4, but when I do @user1.joined_groups.map(&:name), I get 8 records:


#<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')语句,并删除了很多中间代码.

(where i have replaced all BSON::ObjectId('4eab3ca5f11aac2701000009') statements with ones and removed a lot of the middle code.

可在此处找到完整的代码集: https://gist.github.com/1323984

The full set of code is available here: https://gist.github.com/1323984

大多数bizzarre仅仅调用地图可能是导致问题的原因.

Most bizzarre simply calling map might be causing the problem.


    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的宝藏. (更新:不,我在使用标准的蒙古型对象时遇到此错误,因此我完全是在指责蒙古型.)

Thanks for any insight! After repeated attempts, I can't figure out a way in terminal to duplicate this, so I am suspecting the Fabrication gem. (Update: nope, I get this error with standard mongoid objects, so I am totally blaming mongoid.)

蒂姆

推荐答案

我认为问题可能仅仅是因为您没有将组正确地推向用户.尝试使用concat或分别铲除它们.

I think the problem might simply be that you are not pushing the groups onto the user correctly. Try using concat or separately shoveling them.

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

这篇关于从关联创建的重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆