Forum: Ruby on Rails Assign nil to datetime?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Al E. (Guest)
on 2006-12-29 01:08
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. (Guest)
on 2006-12-29 04:50
(Received via mailing list)
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
Al E. (Guest)
on 2006-12-29 05:11
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
This topic is locked and can not be replied to.