Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoid has_and_belongs_to_many associations

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

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.

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

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

like image 523
bonhoffer Avatar asked Nov 14 '22 17:11

bonhoffer


1 Answers

It can be that: https://github.com/mongoid/mongoid/issues/1204

like image 198
sandrew Avatar answered Dec 21 '22 16:12

sandrew