# Empty emtry in database problem

**URL:** <https://www.ruby-forum.com/t/empty-emtry-in-database-problem/141757>\
**Category:** Rails\
**Created:** [June 30, 2008, 1:05pm UTC](https://www.ruby-forum.com/t/empty-emtry-in-database-problem/141757 "2008-06-30T13:05:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![supriyaiiita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@supriyaiiita](https://www.ruby-forum.com/u/supriyaiiita)\
**Post date:** [June 30, 2008, 1:05pm UTC](https://www.ruby-forum.com/t/empty-emtry-in-database-problem/141757/1 "2008-06-30T13:05:34Z")

</div>

Hi,

While adding a new entry in my database , if i dnt enter one of the  
field,  
it gets stored as emtry string and not as NULL field.This creates some  
problem while searching on that model.  
How can i deal with it??

## Thanks, Sup

View this message in context:  
[http://www.nabble.com/empty-emtry-in-database-problem-tp18193221p18193221.html](http://www.nabble.com/empty-emtry-in-database-problem-tp18193221p18193221.html)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![supriyaiiita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@supriyaiiita](https://www.ruby-forum.com/u/supriyaiiita)\
**Post date:** [June 30, 2008, 1:58pm UTC](https://www.ruby-forum.com/t/empty-emtry-in-database-problem/141757/2 "2008-06-30T13:58:49Z")

</div>

Hi  
In migration file you can speciy like  
:null =\> true

If false, the underlying column has a not null constraint added (if the  
database supports it).

You can use like  
t.column :name\_last, :string, :null=\>true (for example)

And already migration file exists you can change column type using  
change\_column

Sijo

---

<div class="post-metadata">

**Author:** ![supriyaiiita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@supriyaiiita](https://www.ruby-forum.com/u/supriyaiiita)\
**Post date:** [June 30, 2008, 2:57pm UTC](https://www.ruby-forum.com/t/empty-emtry-in-database-problem/141757/3 "2008-06-30T14:57:42Z")

</div>

You are probably setting the attribute to an empty string before  
saving the record/model instance. How is the attribute being  
populated?

Empty string is still valid (not null)…

---

<div class="post-metadata">

**Author:** ![supriyaiiita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@supriyaiiita](https://www.ruby-forum.com/u/supriyaiiita)\
**Post date:** [June 30, 2008, 3:16pm UTC](https://www.ruby-forum.com/t/empty-emtry-in-database-problem/141757/4 "2008-06-30T15:16:30Z")

</div>

im not setting it anywhere, it is just getting added through basic code  
of:

@device = Dev.new(@params[:dev])  
and after that checking @device.save and then redirecting\_to some page.

if im not entering anything nothing gets added. my point is it shud get  
added as NULL in database. should’nt it??

---

<div class="post-metadata">

**Author:** ![supriyaiiita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@supriyaiiita](https://www.ruby-forum.com/u/supriyaiiita)\
**Post date:** [June 30, 2008, 2:17pm UTC](https://www.ruby-forum.com/t/empty-emtry-in-database-problem/141757/5 "2008-06-30T14:17:13Z")

</div>

sorry but that i alreayd tried… not working … still saving a field as  
empty in database.!! ☹

On Mon, Jun 30, 2008 at 5:28 PM, Sijo Kg  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![supriyaiiita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@supriyaiiita](https://www.ruby-forum.com/u/supriyaiiita)\
**Post date:** [June 30, 2008, 3:20pm UTC](https://www.ruby-forum.com/t/empty-emtry-in-database-problem/141757/6 "2008-06-30T15:20:38Z")

</div>

On 30 Jun 2008, at 14:15, supriya agarwal wrote:

> im not setting it anywhere, it is just getting added through basic  
> code of:
> 
> @device = Dev.new(@params[:dev])  
> and after that checking @device.save and then redirecting\_to some  
> page.
> 
> if im not entering anything nothing gets added. my point is it shud  
> get added as NULL in database. should’nt it??

By nothing do you mean that there is a corresponding text field which  
you have left empty? That will submit an empty string.  
If you want to coerce those to nulls you’ll have to do it yourself

Fred
