# Validations problem

**URL:** <https://www.ruby-forum.com/t/validations-problem/186601>\
**Category:** Rails\
**Created:** [April 9, 2010, 1:45pm UTC](https://www.ruby-forum.com/t/validations-problem/186601 "2010-04-09T13:45:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 9, 2010, 1:45pm UTC](https://www.ruby-forum.com/t/validations-problem/186601/1 "2010-04-09T13:45:25Z")

</div>

Hello champs,

I have a validation in model ABC and that is:-  
validates\_presence\_of :employee\_id  
Now the record should not be saved without the presence of employee\_id  
right. But now i wrote a test case like this:-

def test\_should\_not\_save\_abcModel\_without\_employee\_id  
abc = Abc.new(:name =\> “ABC Group”)  
assert abc.save, “Saving the record without employee\_id”  
end

But still this record gets saved in database. Why so … ? What i am  
doing wrong here… My database migrations for employee\_id column as  
well:-

t.integer :employee\_id, :null =\> false

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 9, 2010, 3:03pm UTC](https://www.ruby-forum.com/t/validations-problem/186601/2 "2010-04-09T15:03:42Z")

</div>

it is impossible!!

there is something wrong with model or database

2010/4/9 Hemant B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 9, 2010, 4:19pm UTC](https://www.ruby-forum.com/t/validations-problem/186601/3 "2010-04-09T16:19:13Z")

</div>

Anthony wrote:

> it is impossible!!

Hey i know that is impossible… That’s why i am asking you people… i  
have posted my migrations and validations here as well…  
Can you please look at them and tell me …

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 9, 2010, 4:24pm UTC](https://www.ruby-forum.com/t/validations-problem/186601/4 "2010-04-09T16:24:11Z")

</div>

In which file is this test written and how are you running it?

Also, these kinds of tests are not very useful because  
they try to test the framework, ActiveRecord in this case.

Tests should target the intended behavior of your application.

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 10, 2010, 6:07pm UTC](https://www.ruby-forum.com/t/validations-problem/186601/5 "2010-04-10T18:07:32Z")

</div>

You should remove :null =\> false. With it, you have a 0 in any new  
record, so validates\_presence\_of :employee\_id will return true.

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 10, 2010, 10:53pm UTC](https://www.ruby-forum.com/t/validations-problem/186601/6 "2010-04-10T22:53:48Z")

</div>

Are you somehow inserting a blank in your model with a before\_save or  
similar callback? What is the value of the column in the created  
record? Are you the only one with access to the DB tables? Could  
somebody have changed the column to allow now null values?
