title: Rails: model accepts nested attributes but controller doesn't seem to care
body: <p>I'm struggling to get nested attributes down.  Working off of <a href="http://railscasts.com/episodes/196-nested-model-form-part-1" rel="nofollow noreferrer">Railscast 196</a>, I tried to setup my own app that does basic nesting.  Users can create scavenger hunts.  Each hunt consists of a series of tasks (that can belong to any hunt, not just one).  I got a little help <a href="https://stackoverflow.com/questions/9957719/has-and-belongs-to-many-error-development-hunts-tasks-doesnt-exist">here</a> and tried to learn from a post with a <a href="https://stackoverflow.com/questions/2182428/rails-nested-form-with-has-many-through-how-to-edit-attributes-of-join-model">similar issue</a>, but I'm still stuck.  I've been hacking around for hours and I've hit a brick wall.  </p>
  
  <pre><code>class HuntsController &lt; ApplicationController
  
    def index
       @title = "All Hunts"
       @hunts = Hunt.paginate(:page =&gt; params[:page])
    end
  
    def show
      @hunt = Hunt.find(params[:id])
      @title = @hunt.name 
      @tasks = @hunst.tasks.paginate(:page =&gt; params[:page])
    end
  
    def new
      if current_user?(nil) then    
        redirect_to signin_path
      else
        @hunt = Hunt.new
        @title = "New Hunt"
        3.times do
          #hunt =  @hunt.tasks.build 
          #hunt = @hunt.hunt_tasks.build  
          hunt = @hunt.hunt_tasks.build.build_task
        end
      end
    end
  
    def create
      @hunt = Hunt.new(params[:hunt])
      if @hunt.save
        flash[:success] = "Hunt created!"
        redirect_to hunts_path
      else
        @title = "New Hunt"
        render 'new'     
      end
    end
  ....
   end
  </code></pre>
  
  <p>With this code, when I try and create a new hunt, I'm told that there's no method "build_task" (it's undefined).  So when I remove that line and use the second bit of code that's commented out above, I get the error below.  </p>
  
  <pre><code>    NoMethodError in Hunts#new
  
      Showing /Users/bendowney/Sites/MyChi/app/views/shared/_error_messages.html.erb where line #1 raised:
  
      You have a nil object when you didn't expect it!
      You might have expected an instance of ActiveRecord::Base.
      The error occurred while evaluating nil.errors
      Extracted source (around line #1):
  
      1: &lt;% if object.errors.any? %&gt;
      2:   &lt;div id="error_explanation"&gt;
      3:     &lt;h2&gt;&lt;%= pluralize(object.errors.count, "error") %&gt; 
      4:         prohibited this &lt;%= object.class.to_s.underscore.humanize.downcase %&gt; 
  
      Trace of template inclusion: app/views/tasks/_fields.html.erb, app/views/hunts/_fields.html.erb, app/views/hunts/new.html.erb
  </code></pre>
  
  <p>And when I use the first bit of code that's commented out in the hunt controller, then I get an error telling me that my 'new' method has an unintialized constant:</p>
  
  <pre><code>    NameError in HuntsController#new
  uninitialized constant Hunt::Tasks
  </code></pre>
  
  <p>I'm at my wit's end.  Any suggestions on what exactly I'm doing wrong?  Or a strategy  Here are my models:</p>
  
  <pre><code>    class Hunt &lt; ActiveRecord::Base
        has_many :hunt_tasks
        has_many :tasks, :through =&gt; :hunt_tasks #, :foreign_key =&gt; :hunt_id
  
        attr_accessible :name
        validates :name,  :presence =&gt; true,
                          :length   =&gt; { :maximum =&gt; 50 } ,
                          :uniqueness =&gt; { :case_sensitive =&gt; false }
      end
  
      class Task &lt; ActiveRecord::Base
  
        has_many :hunt_tasks
        has_many :hunts, :through =&gt; :hunt_tasks#, :foreign_key =&gt; :hunt_id
  
        attr_accessible :name 
        validates :name,  :presence =&gt; true,
                          :length   =&gt; { :maximum =&gt; 50 } ,
                          :uniqueness =&gt; { :case_sensitive =&gt; false }
  
      end
  
      class HuntTask &lt; ActiveRecord::Base
        belongs_to :hunts # the id for the association is in this table
        belongs_to :tasks
      end
  </code></pre>