Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 覆盖特定型号Rails 4.2的update_all方法_Ruby On Rails_Ruby_Update All_Activerecord Relation - Fatal编程技术网

Ruby on rails 覆盖特定型号Rails 4.2的update_all方法

Ruby on rails 覆盖特定型号Rails 4.2的update_all方法,ruby-on-rails,ruby,update-all,activerecord-relation,Ruby On Rails,Ruby,Update All,Activerecord Relation,我需要重写rails update_all方法,以便它将更新特定模型的updated_at字段 我在app/config/initializer/xyzfile.rb中添加了以下模块: module ActiveRecord class Relation def update_all_with_updated_at(updates) case updates when Hash updates.reverse_merge!(update

我需要重写rails update_all方法,以便它将更新特定模型的updated_at字段

我在app/config/initializer/xyzfile.rb中添加了以下模块:

module ActiveRecord
  class Relation
    def update_all_with_updated_at(updates)
      case updates
        when Hash
          updates.reverse_merge!(updated_at: Time.now)
        when String
          updates += ", updated_at = '#{Time.now.to_s(:db)}'"
        when Array
          updates[0] += ', updated_at = ?'
          updates << Time.now
      end
      update_all_without_updated_at(updates)
    end
    alias_method_chain :update_all, :updated_at
  end
end
模块活动记录
阶级关系
def update_all_和_updated_at(更新)
个案更新
当散列
更新。反向合并!(更新时间:Time.now)
当字符串
更新+=”,更新时间=“{Time.now.to_s(:db)}”
当数组
更新[0]+=',更新位置=?'

更新将以下代码放入文件/config/initializers/update_all_with_touch.rb 将这段代码放入

app/config/initializer/update_all_.rb
类ActiveRecord::关系
def update_all_装饰(更新,条件=nil,选项={})
当前时间=time.now
#将“updated_at”列插入更新中
个案更新
当散列时;更新。合并!(更新时间:当前时间)
当串;更新+=”,更新时间=“#{current_time.to_s(:db)}”

当使用数组时;更新[0]+=',更新位置=?';更新我添加了与此类似的代码,但我想覆盖特定模型的update_all行为,而不是all。为什么不在Time.zone.now调用
updated_的
updated_all
方法(或将其添加到现有哈希)?@JiříPospíšil这是解决方案之一。但是有了这个,我需要在所有必需模型的每个update_all语句中添加updated_at。因此,我正在寻找一种可以覆盖update_all方法的解决方案。
class ActiveRecord::Relation

  def update_all_decorate(updates, conditions = nil, options = {})

    current_time = Time.now

    # Inject the 'updated_at' column into the updates
    case updates
      when Hash;   updates.merge!(updated_at: current_time)
      when String; updates += ", updated_at = '#{current_time.to_s(:db)}'"
      when Array;  updates[0] += ', updated_at = ?'; updates << current_time
    end

  end
  alias_method_chain :update_all, :touch
end