Forum: Ruby on Rails create and update with has_many :through

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5dd60445347fc3dec62474cc732fef0d?d=identicon&s=25 Kyle Mathews (Guest)
on 2006-01-23 20:22
(Received via mailing list)
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
337cdd270761e0e6f4356de45b04d388?d=identicon&s=25 Jonathan Viney (jonny)
on 2006-01-23 21:38
How have you defined your models, and what is the problem you are
having?

-Jonny.

Kyle Mathews 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.
>
>
5dd60445347fc3dec62474cc732fef0d?d=identicon&s=25 Kyle Mathews (Guest)
on 2006-01-23 22:03
(Received via mailing list)
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
2ccb2f7699267d258faaafce4ee997df?d=identicon&s=25 Cody Fauser (Guest)
on 2006-01-23 22:55
(Received via mailing list)
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 Mathews <kyle@titaniumdev.com> wrote:
> end
> undefined method `teachers=' for #<Course:0x243ff00>
> > -Jonny.
> > Posted via http://www.ruby-forum.com/.
> > _______________________________________________
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
http://www.codyfauser.com
This topic is locked and can not be replied to.