Does Rails (ActiveRecord) have a method to delete the contents of a specific column?
When I searched, I couldn't find the information to delete the column itself.
If I don't have it, I'll try to implement it myself, but I can't think of a proper method name.
I am thinking about implementing the following code:
def method name
update(:deleted_at=>nil)
end
I can't think of a proper method name.
It should have some meaning as a business logic or system, so I think it's better to use a name that conveys the intent.
In the code written in the question, deleted_at
is set to nil, so I think this is an intention to undo the record that you logically deleted.
In this case, wouldn't it be better to use a method name that means restoration/resurrection?
(Ruby didn't know the naming convention, so I didn't dare write down the specific method name.)
How about nullify
as a method name?
def nullify (column)
update(column=>nil)
end
You may want to use assign_attributes
instead of update
.
I don't feel the need to create a new method.
As you wrote, you can use update
.
hoge.update(foo:nil)
Any column that can be handled from an ActiveRecord object can be nil.
(Of course, if there is a NOT NULL constraint, it cannot be nil.)
© 2024 OneMinuteCode. All rights reserved.