# Assign nil to datetime?

**URL:** <https://www.ruby-forum.com/t/assign-nil-to-datetime/80589>\
**Category:** Rails\
**Created:** [December 29, 2006, 12:08am UTC](https://www.ruby-forum.com/t/assign-nil-to-datetime/80589 "2006-12-29T00:08:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![al-evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/54ee81/32.png) [@al-evans](https://www.ruby-forum.com/u/al-evans)\
**Post date:** [December 29, 2006, 12:08am UTC](https://www.ruby-forum.com/t/assign-nil-to-datetime/80589/1 "2006-12-29T00:08:34Z")

</div>

I think I’ve seen this mentioned before, but I can’t find it.

In the process of destroying a record, I want to assign nil to a  
datetime field in another record that belongs to it.

However, when I say…

```
self.numbers.each do |n|
  [...]
  n.expires = nil
  [...]
  n.save
end

```

In my before\_destroy method, n.expires doesn’t actually get set to nil,  
either in unit testing or in “real life”.

If I do the same thing in the console, it seems to work:

> > n = Number.find(35)  
> > =\> #\<Number:0x24856b4 @attributes={“expires”=\>“2007-01-27 16:15:14”,  
> > “id”=\>“35”[…]}\>
> 
> > n.expires = nil  
> > =\> nil
> 
> > n.save  
> > =\> true
> 
> > n = Number.find(35)  
> > =\> #\<Number:0x2471358 @attributes={“expires”=\>nil, “id”=\>“35”[…}\>

Can somebody tell me what’s going on here?

–Al Evans

---

<div class="post-metadata">

**Author:** ![al-evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/54ee81/32.png) [@al-evans](https://www.ruby-forum.com/u/al-evans)\
**Post date:** [December 29, 2006, 4:11am UTC](https://www.ruby-forum.com/t/assign-nil-to-datetime/80589/2 "2006-12-29T04:11:16Z")

</div>

Bill W. wrote:

> What does your sql declaration for the field look like? Does it  
> explicitly  
> allow nil values? Just guessing here…

±--------------±-------------±-----±----±--------±---------------+  
| Field | Type | Null | Key | Default | Extra |  
±--------------±-------------±-----±----±--------±---------------+  
| id | int(11) | NO | PRI | NULL | auto\_increment |  
[…]  
| expires | datetime | YES | | NULL | |  
[…]  
±--------------±-------------±-----±----±--------±---------------+

–Al Evans

---

<div class="post-metadata">

**Author:** ![al-evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/54ee81/32.png) [@al-evans](https://www.ruby-forum.com/u/al-evans)\
**Post date:** [December 29, 2006, 3:50am UTC](https://www.ruby-forum.com/t/assign-nil-to-datetime/80589/3 "2006-12-29T03:50:42Z")

</div>

Hi Al,

Al Evans wrote:

> I want to assign nil to a datetime field {…]
> 
> However, when I say…  
> {…]  
> n.expires = nil  
> […]  
> n.expires doesn’t actually get set to nil,  
> either in unit testing or in “real life”.
> 
> If I do the same thing in the console, it seems to work:

What does your sql declaration for the field look like? Does it  
explicitly  
allow nil values? Just guessing here…

Best regards,  
Bill
