I have two models (users and courses) and a JOIN table that allows enrollments in the course:
class User < ActiveRecord::Base
has_many :enrollments, :dependent => :destroy
has_many :courses, :through => :enrollments
end
class Course < ActiveRecord::Base
has_many :enrollments, :dependent => :destroy
has_many :users, :through => :enrollments
end
class Enrollment < ActiveRecord::Base
belongs_to :user
belongs_to :course
end
The enrollments JOIN table has other attributes such as grade, percent completed, etc. However, none of the attributes require input from the user, other than enrolling. Ideally, I’d like to have a new_course_enrollment(@course, {:user_id => current_user} )
link that creates the enrollment in the background (no need for the user to input anything) and redirects back to the courses page, with the “enroll” link replaced by “enrolled” status. Is there a way to do this in the models, without needing to change the default RESTful Enrollments#new controller action?