calling custom validation methods in Rails

前端 未结 3 1848
离开以前
离开以前 2021-01-30 06:42

I just upgraded my rails to 2.3.4 and I noticed this with validations: Lets say I have a simple model Company which has a name. nothing to it. I want to run my own validation:<

3条回答
  •  故里飘歌
    2021-01-30 07:06

    You're getting confused between validations and callbacks.

    Validations are supposed to fail if there are any errors on the object, doesn't matter what the validation returns. Callbacks fail if they return false, regardless if they add any errors to object.

    Rails uses calls valid? from save calls which does not check the result of any validations.

    Edit: Rails treats validate :method as a callback, but valid? still doesn't check for their results, only for errors they added to the object.

    I don't think this behaviour changed at all but I could be wrong. I don't think I've ever written a validation to return false before.

提交回复
热议问题