Rails的复位单列 [英] Rails reset single column

查看:93
本文介绍了Rails的复位单列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个rake任务,将每月运行一次,复位单列。我想preFER的列重置为默认值,但我找不到任何方法来帮助我做到这一点。 reset_column_information不能正常工作

I am trying to implement a rake task that will run once a month to reset a single column. I would prefer to reset the column to its default value but I cannot find any methods to help me accomplish this. reset_column_information does not work

什么是重置活动记录一列的最有效方法是什么?

What is the most efficient way to reset a single column in active record?

推荐答案

基本方法#update_all不更新直接在数据库中,所以它是非常有效的。然而,它绕过回调,因为不加载模型: http://apidock.com/rails / ActiveRecord的/基/ update_all /类

Base method #update_all does the update direct in the database, so it is very efficient. However it bypasses callbacks because the models aren't loaded: http://apidock.com/rails/ActiveRecord/Base/update_all/class

SomeModel.update_all("some_column = 4");  # sets all some_column attributes to 4

重置为默认取决于你如何设置默认摆在首位。如果默认的计算模型,那么你就必须选择和实例的所有记录,这可能是非常缓慢的。如果它在数据库中定义,也许这将是可能的,但我认为这将是数据库特有的。

Resetting it to the default depends on how you are setting the default in the first place. If the default is calculated in the model, then you would have to select and instantiate all the records, which could be very slow. If it's defined in the database, maybe it would be possible but I think it would be database specific.

这篇关于Rails的复位单列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆