On Mon, Mar 06, 2006 at 04:03:06PM +0100, Chris S. wrote:
} > This is the sort of thing I’d do in a database trigger. When you
} > to save, the trigger will get activated and throw a hissy if
} > which will cause ActiveRecord to tell you that the save failed.
} > --Greg
} That sounds like it would work… and like something I don’t want to
} There has to be a way to do it in Rails. I just thought it might be an
} easy thing.
} I suppose I could put it in the view that if the flag is set, don’t
} up a field for a new entry. That doesn’t seem so awful now that I
} about it, except that the rule should go in the model.
} def validate_on_update
} oldobj = find(obj.id);
} if (oldobj.field == 1) and (field == 0)
} errors.add(“field”, “cannot be cleared once set”)
} I know I can get the new values, but is the old object around? I could
} go get the record as it’s already stored, but it seems it might
} exist somewhere.
Well, your other option is to override the field= method in the model.
example, suppose your field is named foo:
fail “Goodbye, cruel world!” if foo == 1 && value == 0
Of course, this assumes that the only way the value of foo is modified
through the mutator method, and I believe that is the case.