Methods for deleting data from columns in rails

Asked 1 years ago, Updated 1 years ago, 87 views

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

ruby-on-rails rails-activerecord

2022-09-30 19:50

3 Answers

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.)


2022-09-30 19:50

How about nullify as a method name?

def nullify (column)
  update(column=>nil)
end

You may want to use assign_attributes instead of update.


2022-09-30 19:50

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.)


2022-09-30 19:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.