Mongoid has_and_belongs_to_many关联 [英] Mongoid has_and_belongs_to_many associations

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

问题描述

我试图让蒙古人来保存关联,但是我只能让一方工作.如果我有以下测试.

I trying to get mongoid to save associations, but I can only get one side to work. If I have the following test.

  test "should add a user as a follower when a user follows the group" do                                                                                                                                        
    @cali_group.followers = []                                                                                                                                                
    @user1.followed_groups << @cali_group                                                                                                                                                  
    assert_equal 1, @user1.followed_groups.count
    assert_equal 1, @cali_group.followers.count
  end

这是失败的,因为@ cali_group.followers为[].我已经使用了一段时间,尝试了@cali_group.reload.但这似乎是在我的代码中执行此操作的唯一方法是同时处理联接的两端,即@cali_group.followers << @user1.如果需要的话,我可以在代码中做到这一点.

Which is failing, because @cali_group.followers is []. I've been working with this for awhile, tried @cali_group.reload. But it looks like the only way to do this in my code is to work both ends of the join, i.e. @cali_group.followers << @user1. I can do that in my code if I have to.

此处是polco_group和用户的模型: https://gist.github.com/1195048

The models for polco_group and user are here: https://gist.github.com/1195048

完整的测试代码在这里: https://gist.github.com/1195052

Full test code is here: https://gist.github.com/1195052

推荐答案

可能是: https://github.com/mongoid/mongoid/issues/1204

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

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