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

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

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:

class Product
  has_many :stock_ins
end

class StockIn
  belongs_to :product
end

You can just use an after_create hook on StockIn:

class StockIn
  belongs_to :product

  after_create :update_product_quantity

  ...

  private

  def update_product_quantity
    product.update(quantity: self.quantity)
  end
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:

class Product
  has_many :stock_ins
end

class StockIn
  belongs_to :product
end

You can just use an after_create hook on StockIn:

class StockIn
  belongs_to :product

  after_create :update_product_quantity

  ...

  private

  def update_product_quantity
    product.update(quantity: self.quantity)
  end
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:

确定