title: How to get fields_for to create new objects and many-to-many relationships simultaneously?
body: <p>I've been following through these Railscasts and trying to amend the code so it works with Rails 3: </p>
  
  <p><a href="http://railscasts.com/episodes/73-complex-forms-part-1" rel="nofollow noreferrer">http://railscasts.com/episodes/73-complex-forms-part-1</a></p>
  
  <p><a href="http://railscasts.com/episodes/73-complex-forms-part-2" rel="nofollow noreferrer">http://railscasts.com/episodes/73-complex-forms-part-2</a></p>
  
  <p><a href="http://railscasts.com/episodes/73-complex-forms-part-3" rel="nofollow noreferrer">http://railscasts.com/episodes/73-complex-forms-part-3</a></p>
  
  <p>I am trying to create Groups, Users and Memberships (the many-to-many relationships) simultaneously. People can add users to the group as they create it and then I want it to route through to a view of the group with all the members. I can get memberships to create just fine but having trouble creating users and associating them the group. My code currently looks like this:</p>
  
  <h1>Group.rb</h1>
  
  <pre><code>class Group &lt; ActiveRecord::Base
    has_many :memberships
    has_many :users, :through =&gt; :memberships
  
    attr_accessible :group_name
  
    def user_attributes=(user_attributes)
      user_attributes.each do |attributes|
        users.build(attributes)
      end
    end
  end
  </code></pre>
  
  <h1>Membership.rb</h1>
  
  <pre><code>class Membership &lt; ActiveRecord::Base
    belongs_to :groups
    belongs_to :users
  end
  </code></pre>
  
  <h1>User.rb</h1>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :memberships
    has_many :groups, :through =&gt; :memberships
  end
  </code></pre>
  
  <h1>groups_controller.rb</h1>
  
  <pre><code>class GroupsController &lt; ApplicationController
    def create #todo test for number of groups they're already in before creation
      @group = Group.new(params[:group])
      @group.memberships.build(:user_id =&gt; current_user.id)
      #@group..build
  
      respond_to do |format|
        if @group.save
          format.html { redirect_to(@group, :notice =&gt; 'Group was successfully created and user added...?') }
        else
          format.html { render :action =&gt; "new" }
        end
      end
    end
  end
  </code></pre>
  
  <p>My form looks like this:</p>
  
  <p><img src="https://cl.ly/1s0R131a0V002y1a1O1Y/Screen_shot_2011-08-31_at_10.11.07.png" alt="The form creator"></p>
  
  <p>Which is created by:</p>
  
  <h1>views/groups/new.html.rb</h1>
  
  <pre><code>&lt;h1&gt;New group&lt;/h1&gt;
  
  &lt;%= form_for(@group) do |f| %&gt;
  &lt;fieldset&gt;
    &lt;legend&gt;Create a new group&lt;/legend&gt;
    &lt;%= render 'shared/group_error_messages', :object =&gt; f.object %&gt;  
    &lt;div class="clearfix"&gt;
      &lt;%= f.label :group_name %&gt;
      &lt;div class="input"&gt;
        &lt;%= f.text_field :group_name %&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
    &lt;div id="users"&gt;
      &lt;%= render :partial =&gt; 'user', :collection =&gt; @group.users %&gt;
    &lt;/div&gt;
  
  &lt;/div&gt;
  &lt;p&gt;&lt;%= add_user_link "Add a member" %&gt;&lt;/p&gt;
  
  
  &lt;div class="actions"&gt;
    &lt;%= f.submit :value =&gt; 'Create your group', :class =&gt; 'btn success'%&gt;
  
    &lt;!-- todo test for if they already have a group...--&gt;
  &lt;/div&gt;
  &lt;/fieldset&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <h1>views/groups/_user.html.rb</h1>
  
  <pre><code>&lt;div class="user"&gt;
    &lt;%= fields_for :user do |f| %&gt;
      &lt;div class="clearfix"&gt;
      &lt;%= f.label :name %&gt;
      &lt;div class="input"&gt;
        &lt;%= f.text_field :name %&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="clearfix"&gt;
      &lt;%= f.label :number %&gt;  
      &lt;div class="input"&gt;
        &lt;%= f.text_field :number %&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;%= link_to_function "remove", "$(this).up('.user').remove()" %&gt;
    &lt;% end %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Thanks <em>very, very</em> much in advance :)</p>