I have the following models,
class Exam
has_many :registrations
end
class Registration
belongs_to: user
belongs_to: lesson
end
class Lesson
has_many :exam_registrations
belongs_to :user
end
class User
has_many :registrations, :through => lessons
end
I'm trying to set up an automated email whereby whenever an Exam is updated an email is delivered to every User who's registered. I've got action mailer set up and working for other non-nested resources but this particular case has stumped me for the past few days.
mailer.rb
def inform_exam_venue(user, student, lesson, exam_registration, exam)
@user = user
@student = student
@lesson = lesson
@exam_registration = exam_registration
@exam = exam
mail(:to => "#{student.name} <#{student.email}>", :subject => "Exam registration update")
end
exams_controller.rb
def update
@exam = Exam.find(params[:id])
@exam_registration = @exam.exam_registrations.find(params[:id])
@lesson = @exam_registration.lesson
@user = User.where("id =? ", @exam_registration.user_id).first
@student = Student.where("id =? ", @exam_registration.student_id).first
respond_to do |format|
if @exam.update_attributes(params[:exam])
Mailer.inform_exam_update(@user, @student, @lesson, @exam_registration, @exam).deliver
format.html { redirect_to @exam, notice: 'Exam was successfully updated.' }
else
format.html { render action: "edit" }
end
end
end
My controller code is obviously wrong, but I cant seem to find any instructions on how to set this up. Please help.
the only thing wrong that I found in your code is that you use in the controller
Mailer.inform_exam_update
instead of
Mailer.inform_exam_venue
So, you need to change _update into _venue