Create and update with has_many :through


#1

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


#2

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.


#3

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


#4

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 wrote:

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

-Jonny.
Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


http://www.codyfauser.com