Rails 4: Is it a bad practice to do database operations in model? -
say have product model(app/models/product.rb) , following method def in model.
def update_stock product product.stock -= 1 product.save(:validation => false) end is acceptable?
thanks in advance.
this looks suspicious me - method in product model, method updates entirely different product instance (the 1 pass in, not 1 call method on).
if like:
class product < activerecord::base def update_stock self.stock -= 1 save end end then more appropriate. (update_stock doesn't seem best name method, either, , skipping validations isn't idea.)
Comments
Post a Comment