# Setting blanks

**URL:** <https://www.ruby-forum.com/t/setting-blanks/128543>\
**Category:** Rails\
**Created:** [February 8, 2008, 4:51pm UTC](https://www.ruby-forum.com/t/setting-blanks/128543 "2008-02-08T16:51:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [February 8, 2008, 4:51pm UTC](https://www.ruby-forum.com/t/setting-blanks/128543/1 "2008-02-08T16:51:20Z")

</div>

I have this form which has a whole bunch of stuff for a user to fill  
out, not all of the fields have to be filled, but the one that are left  
blank I need to enter a dummie value for in the database because all  
that stuff gets entered later on into another form consisting of  
edit\_in\_place fields. The way I have it now is that a method gets called  
after a user profile is created and sets all the values to a dummie  
string if they are blank, the problems is that there are so many field  
in the table that the method is very long because of all the fields.  
Bacisally I’m saying if

if @user.address.blank?  
@user.address = “Empty”

Is there a way to do this which isn’t as long and maybe more  
programmatically correct? Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [February 8, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/setting-blanks/128543/2 "2008-02-08T17:13:23Z")

</div>

i think validates\_each should be, what you’re looking for:

[http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001324](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001324)

something on that line:

validates\_each :email, :phone, :on =\> :create do |record, attr, value|  
if value.blank? …  
end

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [February 8, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/setting-blanks/128543/3 "2008-02-08T22:09:30Z")

</div>

Thorsten M. wrote:

> validates\_each :email, :phone, :on =\> :create do |record, attr, value|  
> if value.blank? …  
> end

I think you are correct and that this is exactly what I want, but it  
doesn’t save the values when I create a new object. I have a model for  
user and am trying to save stuff on save. Here is what I got:

```
 validates_each :company, :on => :save do|record, attr, value|
   if value.blank?
     attr = "Click to Edit"
   end
 end

```

I tried to say record.attr = “Click to Edit” but that didn’t seem to  
work. When I create a new user it seems to run through the whole process

- the validates\_each too, but it doesn’t save. Is there something that  
I’m missing? Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [February 8, 2008, 10:38pm UTC](https://www.ruby-forum.com/t/setting-blanks/128543/4 "2008-02-08T22:38:39Z")

</div>

Why does the value need to be in the database? Why not override the  
display of the column for empty? have it output what every you want.

On Feb 8, 4:09 pm, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [February 8, 2008, 10:43pm UTC](https://www.ruby-forum.com/t/setting-blanks/128543/5 "2008-02-08T22:43:35Z")

</div>

brewpoo wrote:

> Why does the value need to be in the database? Why not override the  
> display of the column for empty? have it output what every you want.
> 
> On Feb 8, 4:09 pm, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Because later on in another part of the form I display the values in  
in\_place\_edit fields and if the values are empty strings then I can’t  
edit the values by clicking on them because there is nothing to click  
on.

-S
