each is a method included with the Enumerable module. Many classes use it, including the internals of many rails classes.
I believe your issue is in the update method of the controller
def update
@project = Project.find(params[:id])
@project.people = Person.find(params[:addpersonid]) if
params[:addpersonid]
if @project.update_attributes(params[:project])
flash[:notice] = 'Project was successfully updated.'
redirect_to :action => 'show', :id => @project
else
render :action => 'edit'
end
end
It seems that you want to replace any existing persons with only one person, but you’ll need to wrap it in an array or similar. You could use
@project.people = [Person.find(params[:addpersonid])] if
params[:addpersonid]
since the = method on the association in a HABTM is looking for multiple objects it will call each on it’s argument to add each person to the association. ie
@project.people = [person1, person2, person3]
Will result somewhere in
[person1, person2, person3].each …
If what your after is to add another person to the projects people then you should use the contat method
@project.people << Person.find(params[:addpersonid]) if
params[:addpersonid]
HTH
Daniel