Hello I am trying to build a multi select to populate a many-to-many join table. I am able to crate the new newform but am getting “AssociationTypeMismatch” when I try to save my record.
The solutions that I am finding on the web are not solving my problem.
Hoping someone can resolve what I should be doing to get rid of “AssociationTypeMismatch”
class Presenter < ActiveRecord::Base
belongs_to :seminar
belongs_to :person
end
class Seminar < ActiveRecord::Base
attr_accessible :description, :title,
has_many :presenters, :foreign_key => "person_id"
has_many :lecturer, :through => :presenters, :source => :person
accepts_nested_attributes_for :lecturer, :presenters
end
class Person < ActiveRecord::Base
attr_accessible :first_name, :last_name
has_many :presentors
has_many :lecturingAt, :through => :presentors
def fullName
first_name + " " + last_name
end
end
seminars_controller.rb
def new
@seminar = Seminar.new
@current_presenters = Person.find(:all)
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @seminar }
end
end
....
def create
@seminar = Seminar.new(params[:seminar])
respond_to do |format|
if @seminar.save
format.html { redirect_to(@seminar, :notice => 'Seminar was successfully created.') }
format.xml { render :xml => @seminar, :status => :created, :location => @seminar }
else
format.html { render :action => "new" }
format.xml { render :xml => @seminar.errors, :status => :unprocessable_entity }
end
end
end
Seminars/_form.html.erb. has which populates my collection select with the names and persion ids of
possible lecurers.
....
<div class="field">
<%= f.label :presenter_id %><br />
<%= collection_select(:seminar,:lecturer,@current_presenters, :id, :fullName,{}, {:multiple=>true} ) %>
....
On submitting the params passed into my controller
Parameters: {...., "seminar"=>{ "lecturer"=>["1", "2"], "title"=>"1234567890", "description"=>"ASDFGHJKL:"}, "commit"=>"Create Seminar"}
Getting error:
ActiveRecord::AssociationTypeMismatch (Instructor(#86075540) expected, got String(#73495120)):.
Try this