我怎样才能正确使用before_save?

时间:2013-02-08 16:59:53

标签: ruby-on-rails ruby-on-rails-3

如果我这样编码就会出现语法错误。
我该如何解决?

before_save :handle_invitation_code, if: :just_confirmed

def just_confirmed?
  confirmed_at_changed? && confirmed_at_was.nil?
end

def handle_invitation_code
  ...
end

1 个答案:

答案 0 :(得分:2)

怎么样?

before_save :handle_invitation_code

def just_confirmed?
  confirmed_at_changed? && confirmed_at_was.nil?
end

def handle_invitation_code
  if self.just_confirmed?
  ...
  end
end

或者您必须将just_confirmed?中的代码作为Proc传递给before_save