I have two models interacting with one another. Each Course has_many Reservations, upon saving the reservation I want to update the Course :course_places attribute to minus the number of reservations that have just been made (equal to the remaining_places method I have created in the reservation controller). I hope that makes sense.
I have been trying to use this answer as inspiration but its not working because the Course update_places! method doesn't know which reservation it's referring to https://stackoverflow.com/a/19169367/6103550
Any suggestions would be warmly received! thanks
Error
undefined method `reservation' for #<Course:0x007f832c1f5020> Did you mean? reservations reservations=
Course Model
class Course < ApplicationRecord
belongs_to :listing, optional: true
has_many :reservations
def update_places!
self.update_column(:course_places, self.reservation.sum(:remaining_places))
end
end
Reservation Model
class Reservation < ApplicationRecord
belongs_to :user
belongs_to :course
belongs_to :listing
after_save :update_course_places
def update_course_places
self.course.update_places!
end
end