保存后回叫不更新

时间:2019-04-24 15:39:20

标签: ruby-on-rails

我正在尝试为我的产品模型设置回调,但是 product 属性的discounted_price不变...

我不能打电话给self.save,它引起了stack level too deep

我在做什么错了?

product.rb

after_save :set_discount

def set_discount
  self.discounted_price = self.price - (self.price * self.discount_percentage / 100)
end

2 个答案:

答案 0 :(得分:2)

您可能希望进行before_save回调,因为after_save发生-顾名思义,保存完成后

答案 1 :(得分:1)

保存到BD中之前,您需要更改值!

library(tidyverse)
DF %>% 
  mutate(n = 1, GenderPeople = str_c("GenderPeople", GenderPeople, sep="_")) %>% 
  spread(GenderPeople, n, fill = 0)
#     ID  YourName GenderPeople_Female GenderPeople_Male
#1 0001      Juan                   0                 1
#2 0002       Ana                   1                 0
#3 0003     Pedro                   0                 1
#4 0004 Alejandra                   1                 0
#5 0005      Alex                   0                 1

此外,before_save :set_discount def set_discount self.discounted_price = price - (price * discount_percentage / 100) end 之后的自我不需要使用