I have a rails app with devise for my user model. In my app users can enroll in courses through enrollments. I added hour_learned and hours_taught. After enrollment I try to add hours learned to the current user but for some reason it just won’t work. I don’t get any error but it just won’t insert into the db. I have an enroll method in my user model that looks like this:
def enroll!(course, user)
enrollments.create!(:course_id => course.id, :active => true)
user.hours_taught = user.hours_taught + (course.hours_long)
end
And yes I made sure the the attributes are accessible. Still no go. Any help would be much appreciated.
Why is this method that you’re defining on the
Usermodel taking auserobject as a parameter? You should define it like this:This method will be defined as an instance method on this class so you can call it like this:
This means that you won’t need to pass through user as an argument this method. Inside the method, we take the
courseobject and assign it to the enrolment, do the hours math and then save it usingsave!, which will raise an exception if the reocrd is invalid (either anActiveRecord::RecordInvalidorActiveRecord::RecordNotSaved)