开发者

ROR ActiveRecord attribute handling with a callback before_update

开发者 https://www.devze.com 2022-12-12 04:02 出处:网络
This code produces an ActiveRecordError: Callbacks must be a symbol denoting the method to call, a string to be

This code produces an ActiveRecordError:

Callbacks must be a symbol denoting the method to call, a string to be evaluated, a block to be invoked, or an object responding to the callback method."

before_update :check_instock, :unless => Proc.new { |inventory| inventory.needed.nil? } 

def check_instock
  if needed < amount
    instock = true
  else
    instock = false
  end
end

This code is placed in my inventory model, I'm trying to handle some logic prior to calling @inventory.update_attributes (controller). Previously I was calling @inventory.upd开发者_StackOverflowate_attributes multiple times, which resulted in code that worked, albeit not succinctly.

Cheers!


before_update :set_instock, :unless => :inventory_not_needed?

private

def set_instock
  self.instock = (needed < amount) ? true : false
end

def inventory_not_needed?
  needed.nil?
end
0

精彩评论

暂无评论...
验证码 换一张
取 消