保存使用rails_admin gem时计算得出的数据。

huangapple go评论89阅读模式
英文:

Saving Calculated Data when using rails_admin gem

问题

在我的项目中,我有product模型和stock_in模型,使用rails_admin宝石,如何在为该产品创建新的stock_in时自动更新产品的数量?

英文:

In my project i have product model and stock_in model, using rails_admin gem how can i update a quantity of a product automatically when new stock_in is created for that product?

答案1

得分: 0

Without seeing any code, it's hard to give you anything more than general advice.

You don't need to do this through rails_admin because Rails already has hooks.

Assuming your Product and StockIn models are related:

  1. class Product
  2. has_many :stock_ins
  3. end
  4. class StockIn
  5. belongs_to :product
  6. end

You can just use an after_create hook on StockIn:

  1. class StockIn
  2. belongs_to :product
  3. after_create :update_product_quantity
  4. ...
  5. private
  6. def update_product_quantity
  7. product.update(quantity: self.quantity)
  8. end
  9. end
英文:

Without seeing any code, it's hard to give you anything more than general advice.

You don't need to do this through rails_admin because Rails already has hooks.

Assuming your Product and StockIn models are related:

  1. class Product
  2. has_many :stock_ins
  3. end
  4. class StockIn
  5. belongs_to :product
  6. end

You can just use an after_create hook on StockIn:

  1. class StockIn
  2. belongs_to :product
  3. after_create :update_product_quantity
  4. ...
  5. private
  6. def update_product_quantity
  7. product.update(quantity: self.quantity)
  8. end
  9. end

huangapple
  • 本文由 发表于 2023年5月14日 01:30:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定