# Trying to make things nicer

**URL:** <https://www.ruby-forum.com/t/trying-to-make-things-nicer/71139>\
**Category:** Rails\
**Created:** [October 5, 2006, 3:33pm UTC](https://www.ruby-forum.com/t/trying-to-make-things-nicer/71139 "2006-10-05T15:33:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ohad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f04885/32.png) [@ohad](https://www.ruby-forum.com/u/ohad)\
**Post date:** [October 5, 2006, 3:33pm UTC](https://www.ruby-forum.com/t/trying-to-make-things-nicer/71139/1 "2006-10-05T15:33:19Z")

</div>

I have a Project model, which has a status property. Status can either  
be 0, 1, 2, or 3 - and complies with the array Project.statuses = [  
:hidden, :available, :in\_progress, :complete ]. This works well in all  
of my application, tested and all. (Project.statuses is only used  
superficially at this point, and I added Project#complete?,  
Project#in\_progress?, etc. - automatically - this works too)

Now, in my unit tests, I often had to set the status of the project  
manually. Now, I could do this in many different ways, but I’ve set my  
eyes on this one:

some\_project.status = :complete

To get this to work, what I did was (in project.rb)

def status=(new\_status)  
if (new\_status.is\_a? Symbol) || (new\_status.is\_a? String)  
new\_status = Project.status\_id\_for new\_status  
end

```
super new_status

```

end

Project.status\_id\_for being a proper lookup (with some aliases, for  
example :assigned is same as :in\_progress). This works perfectly within  
the testing code, and from the console.

However, this code breaks updating using the controller - the  
mostly-scaffold code calls @project.update\_attributes(params[:project])  
(params[:project][‘status’] is set by the form to the proper numeric  
value), but that, for some reason, sets @project.status to nil (which is  
invalid, and I test for with validates\_presence\_of :status).

Now, removing my special status= method brings things back to normal,  
and I could obviously do without this, but… I mean, come on, that’s  
just beautiful. Any chance of this working without an ugly hack?

---

<div class="post-metadata">

**Author:** ![ohad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f04885/32.png) [@ohad](https://www.ruby-forum.com/u/ohad)\
**Post date:** [October 5, 2006, 7:39pm UTC](https://www.ruby-forum.com/t/trying-to-make-things-nicer/71139/2 "2006-10-05T19:39:22Z")

</div>

I don’t know of an easy way to do this…anyone?

## Starr

[www.thebootstrapnation.com](http://www.thebootstrapnation.com)

---

<div class="post-metadata">

**Author:** ![ohad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f04885/32.png) [@ohad](https://www.ruby-forum.com/u/ohad)\
**Post date:** [October 5, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/trying-to-make-things-nicer/71139/3 "2006-10-05T19:41:23Z")

</div>

Actually, now that I think about it, you could use model callbacks to  
do the translation for you before saving and after loading.

## Starr

[www.thebootstrapnation.com](http://www.thebootstrapnation.com)

---

<div class="post-metadata">

**Author:** ![ohad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f04885/32.png) [@ohad](https://www.ruby-forum.com/u/ohad)\
**Post date:** [October 5, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/trying-to-make-things-nicer/71139/4 "2006-10-05T20:17:09Z")

</div>

DOH!!!

Remember kids, numeric values from forms - even selections - get passed  
into the controller as strings. So new\_status was ‘2’, not 2, and  
status\_id\_for(‘2’) == nil.

This code works:

def status=(new\_status) #:nodoc:  
if (new\_status.is\_a? Symbol) || (new\_status.is\_a? String)  
# If we don’t have that status in Project.statuses, leave  
# new\_status alone.  
new\_status = Project.status\_id\_for(new\_status) || new\_status  
end

```
super new_status

```

end
