# Create and update with has\_many :through

**URL:** <https://www.ruby-forum.com/t/create-and-update-with-has-many-through/96343>\
**Category:** Rails\
**Created:** [January 23, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/create-and-update-with-has-many-through/96343 "2006-01-23T20:22:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kylemathews](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/51bf81/32.png) [@kylemathews](https://www.ruby-forum.com/u/kylemathews)\
**Post date:** [January 23, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/create-and-update-with-has-many-through/96343/1 "2006-01-23T20:22:59Z")

</div>

I’m using Edge Rails and the new has\_many :through in my code. What  
I’m trying to do is modify my create and update code to use the new  
relationships. I’m using a table called course\_teachers for the joins.

Here is what I have so far:

```
def create
  @course = Course.new(params[:course])
	@teacher = Teacher.find(params[:primary_teacher])

	@course.teachers = @teacher

  if @course.save
    flash[:notice] = 'Course was successfully created.'
    redirect_to :action => 'show', :id => @course
  else
    render :action => 'new'
  end
end

def update
  @course = Course.find(params[:id])
	# New primary teacher
	@teacher = Teacher.find(params[:primary_teacher])

	# Clear primary teacher for course.
	@course.course_teachers.update_all("is_primary = 0")

	# Need this to add the teacher to the course_teachers join table or

```

update the teacher  
# as primary if they are already associated with this course.  
@course.teachers = @teacher

```
  if @course.update_attributes(params[:course])
    flash[:notice] = 'Course was successfully updated.'
    redirect_to :action => 'show', :id => @course
  else
    render :action => 'edit'
  end
end

```

Thanks,  
Kyle

---

<div class="post-metadata">

**Author:** ![kylemathews](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/51bf81/32.png) [@kylemathews](https://www.ruby-forum.com/u/kylemathews)\
**Post date:** [January 23, 2006, 9:38pm UTC](https://www.ruby-forum.com/t/create-and-update-with-has-many-through/96343/2 "2006-01-23T21:38:09Z")

</div>

How have you defined your models, and what is the problem you are  
having?

-Jonny.

Kyle M. wrote:

> I’m using Edge Rails and the new has\_many :through in my code. What  
> I’m trying to do is modify my create and update code to use the new  
> relationships. I’m using a table called course\_teachers for the joins.

---

<div class="post-metadata">

**Author:** ![kylemathews](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/51bf81/32.png) [@kylemathews](https://www.ruby-forum.com/u/kylemathews)\
**Post date:** [January 23, 2006, 10:03pm UTC](https://www.ruby-forum.com/t/create-and-update-with-has-many-through/96343/3 "2006-01-23T22:03:33Z")

</div>

My models are the following:

class Course \< ActiveRecord::Base  
has\_many :course\_teachers  
has\_many :teachers, :through =\> :course\_teachers  
end

class CourseTeacher \< ActiveRecord::Base  
belongs\_to :course  
belongs\_to :teacher  
end

class Teacher \< ActiveRecord::Base  
has\_many :course\_teachers  
has\_many :courses, :through =\> :course\_teachers  
end

I get the following error if I try to create a new course and add the  
primary teacher.

undefined method `teachers=’ for #Course:0x243ff00

Thanks,  
Kyle

---

<div class="post-metadata">

**Author:** ![kylemathews](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/51bf81/32.png) [@kylemathews](https://www.ruby-forum.com/u/kylemathews)\
**Post date:** [January 23, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/create-and-update-with-has-many-through/96343/4 "2006-01-23T22:55:28Z")

</div>

Kyle,

You need to use the join model class when creating your associations.  
You were trying to set the association’s accessor method to a single  
course object.

Try something like this:

course = Course.create(:name =\> ‘English 101’)  
teacher = Teacher.create(:name =\> ‘Mr. Smith’)

course.course\_teachers.create(:teacher =\> teacher) # Creates and saves  
a new CourseTeacher object.

You could also explicitly create the join model object:  
course\_teacher = CourseTeacher.new(:course =\> course, :teacher =\>  
teacher)  
course\_teacher.save

# Get the associated objects

course.reload # Don’t forget to reload if you didn’t add the object  
through the collection  
course.course\_teachers  
course.teachers

Cody

On 1/23/06, Kyle M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> undefined method `teachers=’ for #Course:0x243ff00
> 
> > -Jonny.  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
[http://www.codyfauser.com](http://www.codyfauser.com)
