多分何かのように:
module ActiveRecord
class Base
def nil!(*names)
unless persisted?
raise ActiveRecordError, <<-MSG.squish
cannot nil on a new or destroyed record object. Consider using
persisted?, new_record?, or destroyed? before nilling
MSG
end
unless names.empty?
changes = {}
names.each do |column|
column = column.to_s
changes[column] = write_attribute(column, nil)
end
primary_key = self.class.primary_key
scope = self.class.unscoped.where(primary_key => _read_attribute(primary_key))
if locking_enabled?
locking_column = self.class.locking_column
scope = scope.where(locking_column => _read_attribute(locking_column))
changes[locking_column] = increment_lock
end
clear_attribute_changes(changes.keys)
result = scope.update_all(changes) == 1
if !result && locking_enabled?
raise ActiveRecord::StaleObjectError.new(self, "nil")
end
@_trigger_update_callback = result
result
else
true
end
end
end
end
が初期化子でいることを入れて、それはあなたがComment.last.nil!(:title)
とコメントのタイトルをゼロもらおう。
'e'が' nil? 'だった場合、 'e.reload'はエラーを起こします。あなたは、(もしそれが存在していれば) '何もない'と期待していることを明確にしてもらえますか?レコードを削除し、現在のインスタンスを埋め込みますか?レコードを破壊するか?あるいは、あなたの質問に 'bar'のような属性を' nil'に設定するだけですか? – spickermann
'e.bar'をnilに設定したいのですか?もしそうなら、 'e.update(bar:nil)'を実行することができます。これで属性が設定され、すべてが1ステップで保存されます。 – moveson
そのユースケースは何ですか? – Shobhit