我试图让 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
这是失败的,因为@cali_group.followers 是 []。我已经用这个工作了一段时间,试过 @cali_group.reload
.但看起来在我的代码中执行此操作的唯一方法是处理连接的两端,即 @cali_group.followers << @user1
.如果需要,我可以在我的代码中这样做。
polco_group 和用户的模型在这里:https://gist.github.com/1195048
完整的测试代码在这里:https://gist.github.com/1195052
最佳答案
关于ruby - Mongoid has_and_belongs_to_many 协会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7308539/