如何更新Sequel数据集的updated_at字段?

时间:2016-03-03 19:03:25

标签: ruby sequel

我正在使用Sequel gem来更新数据集。

如果对模型实例进行更新,则updated_at时间戳会更新得很好:

$Comp = Read-Host 'Please enter a computer name or IP'

If (Test-Connection $Comp -quiet -Count 1) {

    Write-host 'The host responded' -ForegroundColor "yellow"
    Read-Host 'Press any key to continue...' | Out-Null
    Clear-Host

    Get-WmiObject -ComputerName $Comp -class Win32_NetworkLoginProfile | Select- Object Name,@{label='LastLogon';expression={$_.ConvertToDateTime($_.LastLogon)}}

    $TargetPath = '\\skyzone\t$\sky'
    if(-not (Test-Path -Path $TargetPath -PathType Container)) { New-Item -Path $TargetPath -ItemType Directory | Out-Null }

    $Folderlist = ("Pictures","Downloads","Favorites","Documents","Desktop","Videos","Outlook"); 

    Get-ChildItem -Path "\\$Comp\C$\Users\" |
    #Get folders only... And exclude public?
    Where-Object { $_.PSIsContainer -and $_.Name -ne 'Public' } |
    ForEach-Object {
        $user = $_.Name

        foreach ($folder in $Folderlist) {
            $from = Join-Path $_.FullName $folder
            $to = Join-Path $TargetPath "$user\$folder"

            Write-Host -ForegroundColor Green "Processing '$from' To '$to'"
            robocopy $from $to *.*  /MT:16 /XJ *.pst /R:3 /W:1 /NP /e /xf *.vmdk *.vmem *.iso *.exe *.ost desktop.ini /tee /Log+:\\apgrwkate1090f\t$\dren\$user\backup.txt
        }
    }
}

但是,当您对数据集调用批量更新时,它不会更新每个条目的updated_at时间戳。

foo = Foo[id: 1] foo.update(blah: blah)

我查看了我在代码中使用的Sequel docs regarding the timestamps plugin,但仍然没有使用批量更新更新updated_at字段。有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

运行Foo.where(id: 1).update(blah: blah)对数据库执行单个查询,该查询不会运行模型实例级挂钩。您可以单独更新每条记录:

Foo.where(id: 1).all{|f| f.update(blah: blah)}

或者您可以切换到基于数据库触发器的方法,例如https://github.com/jeremyevans/sequel_postgresql_triggers

答案 1 :(得分:0)

你可以看一下update_all函数。它是一个活动的记录方法,因此它应该触发updated_at。

更新:好吧我刚检查过它没有触发,你应该用他的答案!