title: Rails 3, nested multi-level forms and has_many through
body: <p>I'm trying to get it to work but it dosen't!</p>
  
  <p>I have </p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :events, :through =&gt; :event_users
    has_many :event_users
    accepts_nested_attributes_for :event_users
  end
  
  class Event &lt; ActiveRecord::Base
    has_many :event_users
    has_many :users, :through =&gt; :event_users
    accepts_nested_attributes_for :users
  end
  
  class EventUser &lt; ActiveRecord::Base
    set_table_name :events_users
    belongs_to :event
    belongs_to :user
    accepts_nested_attributes_for :events
    accepts_nested_attributes_for :users
  end
  </code></pre>
  
  <p>And also the table-layout</p>
  
  <pre><code>event_users
    user_id
    event_id
    user_type
  events
    id
    name
  users
    id
    name
  </code></pre>
  
  <p>And this is my form</p>
  
  <pre><code>&lt;%= semantic_form_for @event do |f| %&gt;
    &lt;%= f.semantic_fields_for :users, f.object.users do |f1| %&gt;
      &lt;%= f1.text_field :name, "Name" %&gt;
      &lt;%= f1.semantic_fields_for :event_users do |f2| %&gt;
        &lt;%= f2.hidden_field :user_type, :value =&gt; 'participating' %&gt;
      &lt;% end %&gt;
    &lt;% end %&gt;
  &lt;%= link_to_add_association 'add task', f, :users %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>The problem is that if I create a new user this way, it doesn't set the value of user_type (but it creates a user and a event_users with user_id and event_id). If I go back to the edit-form after the creation of a user and submit, then the value of user_type is set in events_users. (I have also tried without formtastic)
  Any suggestions? Thanks!</p>
  
  <p>----edit----</p>
  
  <p>I have also tried to have the event_users before users</p>
  
  <pre><code>&lt;%= semantic_form_for @event do |f| %&gt;
    &lt;%= f.semantic_fields_for :event_users do |f1| %&gt;
      &lt;%= f1.hidden_field :user_type, :value =&gt; 'participating' %&gt;
      &lt;%= f1.semantic_fields_for :users do |f2| %&gt;
        &lt;%= f2.text_field :name, "Name" %&gt;
      &lt;% end %&gt;
    &lt;% end %&gt;
  &lt;%= link_to_add_association 'add task', f, :event_users %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>but then it only throws me an error: </p>
  
  <blockquote>
    <p>User(#2366531740) expected, got
    ActiveSupport::HashWithIndifferentAccess(#2164210940)</p>
  </blockquote>
  
  <p>--edit-- </p>
  
  <p>the link_to_association is a formtastic-cocoon method (https://github.com/nathanvda/formtastic-cocoon) but I have tried to do other approaches but with the same result</p>
  
  <p>---edit----</p>
  
  <pre><code>def create
    @event = Event.new(params[:event])
    respond_to do |format|
      if @event.save
        format.html { redirect_to(@event, :notice =&gt; 'Event was successfully created.') }
        format.xml  { render :xml =&gt; @event, :status =&gt; :created, :location =&gt; @event }
      else
        format.html { render :action =&gt; "new" }
        format.xml  { render :xml =&gt; @event.errors, :status =&gt; :unprocessable_entity }                 
      end
    end
  end
  </code></pre>