Rails how to update a column after saving?

前端 未结 5 2088
栀梦
栀梦 2020-12-15 08:09

I want to create an after_save method for my rate action. It would divide rating_score/ratings and update the column rating.

class KonkurrancersController &         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 08:44

    Any update_attribute in an after_save callback will cause recursion, in Rails3+. What should be done is:

    after_save :updater
    # Awesome Ruby code
    # ...
    # ...
    
    private
    
      def updater
        self.update_column(:column_name, new_value) # This will skip validation gracefully.
      end
    

提交回复
热议问题