Hey I am stuck with my orientation in rails.
I got a User model, a Course Model and a CourseEnrollment Model.
When I want to add a link in my Course Index View like
link_to 'join' CourseEnrollment.create(:course_id => course.id, :user_id => current_user)
Does this create method belong to my Model? I am confused because in my User Model I defined a method that uses role_assignments.create(.....). What is the difference between these 2 create methods? I cant use course_enrollments.create by the way. Thx for your time
I’m a bit confused as to what you’re asking, but I’ll try my best.
(First of all, in your example,
current_usershould probably becurrent_user.id.)When you call
CourseEnrollment.create, you are simply creating a newCourseEntrollmentmodel with the specified attributes.Assuming that your
Usermodelhas_many :role_assignments:When you call
@role_assignments.createfrom within yourUsermodel, Rails automatically creates the association for you (e.g. sets theuser_idto theidof the user). This doesn’t have to be done within the model itself, though:current_user.role_assignments.create(...) # automatically sets the associationAssuming that your
Usermodel alsohas_many :course_enrollments, the following will create aCourseEnrollmentmodel and automatically associate it with the current user:current_user.course_enrollments.create(...)