# Validation question

**URL:** <https://www.ruby-forum.com/t/validation-question/196585>\
**Category:** Rails\
**Created:** [January 29, 2008, 3:56am UTC](https://www.ruby-forum.com/t/validation-question/196585 "2008-01-29T03:56:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [January 29, 2008, 3:56am UTC](https://www.ruby-forum.com/t/validation-question/196585/1 "2008-01-29T03:56:35Z")

</div>

I have a courses model that has two fields:

courses(title, members\_count)

Is it possible to have a validation that only lets the user update the  
title if members\_count == 0?

def validate  
errors.add(:title, “cannot be changed once users have signed up”) if  
members\_count \> 0 and title\_changed  
end

But I’m not sure how to check that the title has changed - any help is  
appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [January 29, 2008, 5:36pm UTC](https://www.ruby-forum.com/t/validation-question/196585/2 "2008-01-29T17:36:32Z")

</div>

I’ve never liked the solutions I’ve come up with… but from a  
practical perspective you might simply disable the text\_field if  
member\_count\>0.

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [January 29, 2008, 5:44pm UTC](https://www.ruby-forum.com/t/validation-question/196585/3 "2008-01-29T17:44:51Z")

</div>

Create a hidden field old\_title. You’ll need to add an attr\_accessor,  
but then you should be able to check if old\_title == title. But I  
liked the other suggestion better… why have the text field if it  
can’t be changed? Perhaps you should render a partial that only shows  
the enabled text field if member\_count\<0

-Ryan

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [January 29, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/validation-question/196585/4 "2008-01-29T17:46:40Z")

</div>

I thought about something like that, but I think it would fairly easy  
to bypass - all the user would need to do is change the form and  
submit it to change the title regardless of members\_count. Any other  
ideas? My guess is that this is a fairly common type of validation  
people need to perform…

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [January 30, 2008, 8:43pm UTC](https://www.ruby-forum.com/t/validation-question/196585/5 "2008-01-30T20:43:27Z")

</div>

I got something that works by saving the previous title from the  
controller:

def update  
@course = Course.find params[:id]  
@course.save\_previous\_title  
@course.attributes = params[:course]  
@course.save!  
flash[:notice] = “Class #{@course.title} updated”  
redirect\_to\_course  
rescue ActiveRecord::RecordInvalid  
render :action =\> :new  
end

and in models/course.rb

attr\_accessor :previous\_title  
def save\_previous\_title  
self.previous\_title = title  
end

def validate  
errors.add(:title, “cannot be changed once users have signed up”)  
if members\_count \> 0 and title != previous\_title  
end

It doesn’t feel very clean though - anyone know if I can set a  
callback to save\_previous\_title in the model before it updates itself  
so I don’t have to call it explicitly from the controller or if there  
is a better way to go about this?
