Skip to content

Commit fc9db86

Browse files
authored
Merge pull request #39 from dalvarez2596/lesson-84
Lesson 84
2 parents 72e23c9 + b5f172a commit fc9db86

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

app/models/enrollment.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class Enrollment < ApplicationRecord
2-
belongs_to :course
2+
belongs_to :course, counter_cache: true
3+
# Course.find_each{ |course| Course.reset_counters(course.id, :enrollments)}
34
belongs_to :user
45

56
validates :user, :course, presence: true

app/views/courses/_course.html.haml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
= review_button(course)
1212
.card-footer
1313
.text-warning.fa.fa-star
14-
= course.average_rating
14+
%b= course.average_rating
15+
-# Enrollments count:
16+
-# = course.enrollments.count
17+
Counter cache enrollments count:
18+
= course.enrollments_count
1519
.card-footer
1620
.fa.fa-list-check
1721
= course.lessons.count
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddCounterCacheToModels < ActiveRecord::Migration[7.2]
2+
def change
3+
add_column :courses, :enrollments_count, :integer, null: false, default: 0
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)