I’ve got posts that have an optional “expire_at” datetime.
I’m trying to save this datetime from my form if a “custom_expire_at” checkbox is checked. If it is not checked, the datetime should be set to NULL.
I’ve tried trashing the expire_at parameters if the checkbox value is not there:
# Delete expiration dates if box is not checked
if params[:item][:custom_expire_at]
params[:item].delete(:custom_expire_at)
else
params[:item].delete(:expire_at)
end
However, expire_at is multi-parameter:
"expire_at(1i)"=>"2011", "expire_at(2i)"=>"2", "expire_at(3i)"=>"10", "expire_at(4i)"=>"21", "expire_at(5i)"=>"32"
Trying to avoid completely destroying this datetime select via jQuery — would much rather pop it out of the params.
In summary:
1) How can I strip out these parameters if I don’t want them?
2) even if I managed to strip out expire_at, I don’t know how to set a value back to NULL. If I set expire_at to nil, my model will ignore the update.
def ensure_expire_at_has_a_value
self.expire_at ||= nil
end
Any ideas? Thanks in advance!
To get around the multi-parameter you could use delete_if to delete all expire_at parameters:
params.delete_if{ |key, value| key.match(/^expire_at/) }