Given the following code, which enroll method is better and why? Or should this code be improved some other way altogether?
My idea of “better” for the above basically boils down to 1) most philosophically correct (best practices) and 2) most efficient/performant.
Class Course < ActiveRecord::Base
has_many :enrollments # basically a join table
has_many :students, :source => :user, :through => :enrollments
def enroll_this_way(student)
self.enrollments << Enrollment.new(:course_id => self.id, :student_id => student.id)
end
# OR
def enroll_that_way(student_id)
self.enrollments << Enrollment.new(:course_id => self.id, :student_id => student_id)
end
end
1 Answer