title: Rails model error AssociationTypeMismatch expected, got String
body: <p>I'm attempting to create a Rails 3 app with three models, with <code>has_many</code> and <code>belongs_to</code> relationships. I'm getting an <code>AssociationTypeMismatch</code> error when I try to submit a form. Specifically <code>Location(#70232625418240) expected, got String(#70232609906560)</code> The models I've set up are: </p>
  
  <ul>
  <li>Food</li>
  <li>Location</li>
  <li>Month</li>
  </ul>
  
  <p>Right now I'm working on connecting the Food and Location models. I've created a simple form form <code>@food</code> which pulls in the entries from <code>@location</code> as checkboxes. I'd like to select different locations by checking them off, and associate them with the <code>locations</code> index in my Food model.</p>
  
  <p>I'm trying to have the checkboxes submit as an array, so I can associate the locations with the foods. So I'm submitting the location ids, which I believe will allow me to pull in the location Name for each one associated with a food entry.</p>
  
  <p>My models are set up this way: </p>
  
  <p><strong>food.rb</strong></p>
  
  <pre><code>class Food &lt; ActiveRecord::Base
    attr_accessible :area, :description, :icon, :iconSource, :image, :locations, :months, :name, :source, :type, :month_id
    has_many :locations
    has_many :months
  end
  </code></pre>
  
  <p><strong>location.rb</strong></p>
  
  <pre><code>class Location &lt; ActiveRecord::Base
    attr_accessible :city, :region, :regionName, :state, :title, :food_id, :month_id, :locations_id
    has_many :months
    belongs_to :foods
  end
  </code></pre>
  
  <p>The form I have set up looks like this: </p>
  
  <p><strong>new.html.erb</strong></p>
  
  <pre><code>&lt;%= form_for @food do |f| %&gt;
    &lt;%= render 'shared/error_messages', object: f.object %&gt;
      &lt;div class="field"&gt;
          &lt;%= f.text_field :name, placeholder: "Name", :class =&gt; 'field-name' %&gt;
      &lt;/div&gt;
  
      &lt;% for location in Location.find(:all) %&gt;
          &lt;div&gt;
            &lt;%= check_box_tag "food[locations_id][]", location.id %&gt;
            &lt;%= location.title %&gt;
          &lt;/div&gt;
      &lt;% end %&gt;
  
      &lt;%= f.submit "Post" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>The Foods controller is using the create action to process the form:</p>
  
  <p><strong>foods_controller.rb</strong></p>
  
  <pre><code>class FoodsController &lt; ApplicationController
    def index
      @foods = Food.all
    end
    def new
      @food = Food.new
      @locations = Location.all
    end
    def create
      @food = Food.new(params[:food])
      if @food.save
          redirect_to foods_url(@food), :notice =&gt; "Food created!"
      else
        render :action =&gt; "new"
      end
    end
  end
  </code></pre>
  
  <p>The form submits just fine when I'm only entering data relating to the Foods model. But when I select one the of Location checkboxes and try to submit the form, I'm getting this <code>Location(#70232625418240) expected, got String(#70232609906560)</code> error. Here's the rest of the output:</p>
  
  <pre><code>app/controllers/foods_controller.rb:10:in `new'
  app/controllers/foods_controller.rb:10:in `create'
  
  Parameters:
  
  {"utf8"=&gt;"✓",
   "authenticity_token"=&gt;"QThXhDG8pPJcRVTTW6FXmo6MhGcoUeUspBhRtrbsbig=",
   "food"=&gt;{"name"=&gt;"Apple",
   "locations"=&gt;["1"]},
   "commit"=&gt;"Post"}
  </code></pre>
  
  <p>After looking into this for a few hours, I have tried everything I could find to fix this. I realize that I <a href="https://stackoverflow.com/questions/5716412/country-expected-got-string-error">need to be using</a> the <code>location_id</code> instead of <code>locations</code>. But after that, I'm turning up empty on other solutions here. Has anyone had a similar issue?</p>