mongoid/mongoid — Issue #1685: build on belongs_to relationships
Author: jamesnelson
Created: 2012-02-08 08:51:25+00:00
State: open

It would be useful if you call build_ for a belongs_to relationship. Here is a use case for it:

```ruby
class User < Person
include Mongoid::Document

has_many :memberships, autosave: true
end

class Membership
include Mongoid::Document

belongs_to :account, autosave: true
belongs_to :user, autosave: true

# Other data...
field :roles, type: Integer
end

class Account
include Mongoid::Document

has_many :memberships, autosave: true
end
```

```ruby
user = User.new(...)
membership = user.memberships.build(roles: 1)
account = membership.build_account(...)
```

With build_account, it would be a lot easier to create a new user form with all three models in rails, as I could just use accepts_nested_attributes_for. As it is, I can't find an easy way to model this kind of relationship that well. has_and_belongs_to_many won't work, as the table acting as a join table actually has data itself.