更新 rails 中的数据库列

Update database column in rails

我有一个问题, 我想更新数据库的 "active" 列。 我的架构 =

 tablename(name:string , id:int , active:0 ,created_at)

我想在一段时间后更新我的 rails 应用程序中的活动属性,比方说从 created_at 时间

开始的 2 天

我已经从控制器尝试过一些这样的东西,但是 none 这对我有用.. 有人可以帮忙吗?

def check_aim
  @aim = Aim.find(1)

  aimdays = @aim.day
  futuredate = @aim.created_at+aimdays.days
  if(Time.now == futuredate)
    @aim.active = false # i have tried @aim.update_attribute(:active,1) also didn't word either
    @aim.save
  end
  if @aim.save
    saved
  else
    not saved  
  end
end
helper_method :check_aim

查看class

=debug check_aim

这个returns

saved

但是当我看到我的数据库时没有任何改变...请帮助

要在一定时间后真正更新列,一种方法是 运行 cron 脚本。该脚本可以每天 运行 检查 table 并更新 created_at 日期后 2 天未设置为活动的 active 字段。

有可能条件 Time.now == futuredate 失败了,但 @aim.save 仍然会 return 你 true 因为它可以不加改变地保存。

您需要将其更改为 Time.now >= futuredate 因此如果 futuredate 通过 active 将设置为 0

您可以将 puts 语句移动到块中并检查它是否正在打印某些东西

def check_aim
  @aim = Aim.find(1)

  aimdays = @aim.day
  futuredate = @aim.created_at + aimdays.days
  if(Time.now >= futuredate)
    @aim.active = 0 # i have tried @aim.update_attribute(:active,1) also didn't word either
    if @aim.save
      "saved"
    else
      "not saved"
    end
  end
end

我想出来了..愚蠢rails

伙计们,这是 rails 最愚蠢的事情... 无非是关键词'active'的问题 我将列名从 active 更改为 status,现在它可以正常工作了。 该死的!我在这上面浪费了几个小时:/