英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论