Mongoid has_and_belongs_to_many关联

bon*_*fer 5 ruby mongoid ruby-on-rails-3 ruby-on-rails-3.1

我试图让mongoid保存关联,但我只能让一方工作.如果我有以下测试.

  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
Run Code Online (Sandbox Code Playgroud)

哪个失败了,因为@ cali_group.followers是[].我已经使用了一段时间,尝试过@cali_group.reload.但看起来在我的代码中执行此操作的唯一方法是使用连接的两端,即@cali_group.followers << @user1.如果必须的话,我可以在我的代码中这样做.

polco_group和用户的模型如下:https://gist.github.com/1195048

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