mongoid/mongoid — Issue #1437: Bug when setting two referenced relations on the same models
Author: hartator
Created: 2011-11-18 12:45:14+00:00
State: open

I come across a bug which look like mine (:https://github.com/mongoid/mongoid/issues/1434) but I don't manage to find a way to work around.

```
class Category
include Mongoid::Document
has_and_belongs_to_many :games_tagged, class_name: 'GameSpec', index: true
has_many :main_games, class_name: 'GameSpec'
end

class GameSpec
include Mongoid::Document
has_and_belongs_to_many :tags, class_name: 'Category', index: true
belongs_to :main_category, class_name: 'Category', index: true
end

[45] pry(main)> game_spec.main_category = Category.last
NoMethodError: undefined method `compact' for #
from /Users/ioxtrem/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.1.1/lib/active_model/attribute_methods.rb:385:in `method_missing'
```

I have run a few tests and it doesn't seems to like the fact that I define the Category/GameSpec relations twice with two different macros. (It was an ActiveRecord relation before and it worked fine.)

Anyway, I love to use mongoid, great job, thanks durran.

Using : Ruby 1.9.2, Rails 3.1.1, Mongoid 2.3.3, Mongo Ruby Driver 1.4.0 and MongoDB 2.0.1