开发者

Rails3: Make update/create fail from model?

开发者 https://www.devze.com 2023-03-16 03:24 出处:网络
There\'s got to be an easy way to do this, but I cannot find an answer... When some creates or updates a WorkRequest in开发者_高级运维 my app, I do other processing, including creating a Workflow obj

There's got to be an easy way to do this, but I cannot find an answer...

When some creates or updates a WorkRequest in开发者_高级运维 my app, I do other processing, including creating a Workflow object. I do some checking to make sure, for example, there isn't more than one Workflow already created for this WorkRequest. If there is, I want the update/create to fail with an error message. I just can't see how to do this. I tried returing false from my before_update callback method, but this did not work.

Do I raise an error and rescue it in my controller? What is the right way to do this in Rails 3?

Any help would be much appreciated.


This is why you have validations. You can implement an own validation like this:

class ...
  validate :my_validation

  def my_validation
    if workflows > 1
      errors.add(:workflow, "cannot be more than one" )
    end
  end
end
0

精彩评论

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