Edit #1
Here is what I have now
<% if course.complete? %>
<%= link_to "Completed", course, class: "block text-lg w-full text-center text-white px-4 py-2 bg-green hover:bg-green-dark border-2 border-green-dark leading-none no-underline" %>
<% else %>
<%= link_to "View Modules", course, class: "block text-lg w-full text-center text-grey-dark hover:text-darker px-4 py-2 border-2 border-grey leading-none no-underline hover:border-2 hover:border-grey-dark" %>
<% end %>
course_module.rb
class CourseModule < ApplicationRecord
extend FriendlyId
friendly_id :title, use: :slugged
belongs_to :course
has_many :course_exercises
validates :title, :course_id, presence: true
scope :completed, -> { where(complete: true) }
after_save :course_completed
private
def course_completed
course = course_module.course
course.update(complete: true) if course.course_modules.all?(&:complete?)
end
end
The names match so no issues there, however, how would I go about calling the model method?
Original Question
Currently, I have courses and within the course I have modules both have complete columns which are a boolean, currently, students can complete modules by clicking on the tick like so
I have the courses database like so
and the modules table like so
But I want the functionality to work so when the last module is marked as complete, the course itself gets marked as complete in the database, currently in the course_modules_controller.rb
I have
def complete
@course_module = CourseModule.friendly.find(params[:id])
@course_module.complete = true
@course_module.save
redirect_to courses_path, notice: 'Module completed, congratulations!'
end
This works for the modules, but I want to mark the course as complete if the last task has been marked as complete.
all?
but the direction is right. – Massarelli