I have a User model that has_many :comments
and a Comment model that belongs_to :user
.
I have want to gather comment.ratings
to set user.rating
so I have an after_save
method in my Comment model that I call. However this never gets called.
Here is the method:
def update_user_rating
user = User.find(self.is_about)
user.trip_rating = (Comment.find_all_by_is_about(self.is_about).collect { |comment| comment.rating }.sum) /
Comment.find_all_by_is_about(self.is_about).count
end
I checked the ruby code and it works on the console, so I assume its not the ruby code.
Yes I do have after_save :update_user_rating in the model as well.
I am trying to understand what I am doing wrong?
EDIT: Based on the debugger I saw that the first part of the user.trip_rating= is never executing. Meaning I tried to do next
on that line but it never went through so I am assuming that line is the problem. However checking in rails console again, I saw that it works there. I don't understand!
SOLVED: Here is how I solved the problem although I don't like it. I removed the after_save callback and added a method to the user model to calculate its own ratings, and called it in the controller.