# One-way change

**URL:** <https://www.ruby-forum.com/t/one-way-change/77135>\
**Category:** Rails\
**Created:** [March 6, 2006, 4:01am UTC](https://www.ruby-forum.com/t/one-way-change/77135 "2006-03-06T04:01:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [March 6, 2006, 4:01am UTC](https://www.ruby-forum.com/t/one-way-change/77135/1 "2006-03-06T04:01:46Z")

</div>

I’m developing an app, and I want to make a single boolean field OK to  
change from 0 to 1, but not allow 1 to 0.

Seems like the model’s validate method would be the right place for  
this, but I’m not sure how to implement it.

Any suggestions (actual or pointers to) are greatly appreciated.

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [March 6, 2006, 3:34pm UTC](https://www.ruby-forum.com/t/one-way-change/77135/2 "2006-03-06T15:34:15Z")

</div>

On Mon, Mar 06, 2006 at 04:01:46AM +0100, Chris S. wrote:  
} I’m developing an app, and I want to make a single boolean field OK to  
} change from 0 to 1, but not allow 1 to 0.  
}  
} Seems like the model’s validate method would be the right place for  
} this, but I’m not sure how to implement it.  
}  
} Any suggestions (actual or pointers to) are greatly appreciated.

This is the sort of thing I’d do in a database trigger. When you attempt  
to  
save, the trigger will get activated and throw a hissy if necessary,  
which  
will cause ActiveRecord to tell you that the save failed.

–Greg

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [March 6, 2006, 4:03pm UTC](https://www.ruby-forum.com/t/one-way-change/77135/3 "2006-03-06T16:03:06Z")

</div>

> This is the sort of thing I’d do in a database trigger. When you attempt  
> to save, the trigger will get activated and throw a hissy if necessary,  
> 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 do.  
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 put  
up a field for a new entry. That doesn’t seem so awful now that I think  
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”)  
end  
end

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 already  
exist somewhere.

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [March 6, 2006, 4:44pm UTC](https://www.ruby-forum.com/t/one-way-change/77135/4 "2006-03-06T16:44:12Z")

</div>

> Well, your other option is to override the field= method in the model.  
> For example, suppose your field is named foo:
> 
> def foo=(value)  
> fail “Goodbye, cruel world!” if foo == 1 && value == 0  
> super(value)  
> end
> 
> Of course, this assumes that the only way the value of foo is modified  
> is through the mutator method, and I believe that is the case.

Brilliant. This should do the job exactly. Thanks!

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [March 6, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/one-way-change/77135/5 "2006-03-06T16:38:15Z")

</div>

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  
attempt  
} \> to save, the trigger will get activated and throw a hissy if  
necessary,  
} \> 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  
do.  
} 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  
put  
} up a field for a new entry. That doesn’t seem so awful now that I  
think  
} 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”)  
} end  
} end  
}  
} 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  
already  
} exist somewhere.

Well, your other option is to override the field= method in the model.  
For  
example, suppose your field is named foo:

def foo=(value)  
fail “Goodbye, cruel world!” if foo == 1 && value == 0  
super(value)  
end

Of course, this assumes that the only way the value of foo is modified  
is  
through the mutator method, and I believe that is the case.

–Greg
