How to create has_many_through associations with a nested form
===

What i am trying to do is create a *Asset* that can have multiple *Users* added as *Viewers* during the creation of the *Asset*

It is my understanding that i should be using `accepts_nested_attributes_for` and have a `text_field` for each viewer. the problem is that i want to look up the *User* by there email address.

In a non nested situation i have followed the railscast for [Create Model Through Text Field][1] which uses `attr_accessor :user_email`.

My problem is that i can't seem to make `attr_accessor :user_email` and `accepts_nested_attributes_for` play well together.

thank you so much for your time.

**asset.rb**

    class Asset < ActiveRecord::Base 
      belongs_to :team
      has_many :viewers, :dependent => :destroy
      has_many :users, :through => :viewers

      attr_accessible :title, :about, :image, :web_video, :full_resolution_video, :team_id, :author, :tag_tokens, :collection_tokens, :viewers_attributes  
      accepts_nested_attributes_for :viewers, allow_destroy: true
      attr_accessor :viewers_attributes
      attr_reader :tag_tokens, :collection_tokens
  
    ...
    end

**viewer.rb**

    class Viewer < ActiveRecord::Base
      belongs_to :asset
      belongs_to :user
      accepts_nested_attributes_for :user
      attr_accessible :user_id, :asset_id
      attr_accessor :user_email
  
      validates_presence_of :user_id, on: :create
    
      def make_new_user
        create_user(:email => user_email)
      end
    end

**form**

    <%= form_for [@team, @asset], :html => {:multipart => true} do |f| %>
      ...
      <%= f.fields_for :viewers do |v| %>
        <%= v.text_field :user_email %>
        <%= v.check_box :_destroy %>
        <%= v.label :_destroy, "Remove" %>
      <% end %>
      ...
    <%end%>

**asset_controller**

    class AssetsController < ApplicationController
      ...
      def new
        @asset = Asset.new
        @team = Team.find(params[:team_id])
        3.times { @asset.viewers.build}
      end
      ...
    end

**viewer_controller**

    class ViewersController < ApplicationController
      ...
      def create
        @viewer = Viewer.new(params[:viewer])
        @asset = Asset.find(params[:viewer][:asset_id])
        @viewer.user = User.find_by_email(:email => params[:viewer][:user_email])
        if @viewer.save
          redirect_to edit_team_asset_path(@asset.team,:id => @asset.id), :notice => "New viewer created."
        else
          redirect_to edit_team_asset_path(@asset.team,:id => @asset.id), :alert => "Fail."
        end
      end
      ...
    end


  [1]: http://railscasts.com/episodes/57-create-model-through-text-field