Assign nil to datetime?

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

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

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