Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.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/6/codeigniter/3.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:如何使用列值的日期范围定义范围?_Ruby On Rails_Rails Activerecord - Fatal编程技术网

Ruby on rails RAILS:如何使用列值的日期范围定义范围?

Ruby on rails RAILS:如何使用列值的日期范围定义范围?,ruby-on-rails,rails-activerecord,Ruby On Rails,Rails Activerecord,我正在尝试创建一个命名范围,我希望在其中接收仅在创建后第一周内更新的记录。 正如您所看到的,我收到了一条错误消息,该消息针对在创建的列名 scope :shortlived, where("updated_at < ?", (created_at+7.days)) undefined local variable or method `created_at' for #<Class:0xb184725c> 范围:短期,其中(“更新时间{where('updated_at

我正在尝试创建一个命名范围,我希望在其中接收仅在创建后第一周内更新的记录。 正如您所看到的,我收到了一条错误消息,该消息针对在创建的列名

scope :shortlived, where("updated_at < ?", (created_at+7.days))


undefined local variable or method `created_at' for #<Class:0xb184725c>
范围:短期,其中(“更新时间<?”,(创建时间+7天))
未定义的局部变量或方法“created_at”#

如果您能告诉我可能有什么问题,我将不胜感激。

您可以使用特定于数据库的内容。例如,对于MySQL:

scope :shortlived, -> { where('updated_at < created_at + interval 3 day') }
scope:shortlife,->{where('updated_at
您可以使用特定于数据库的内容。例如,对于MySQL:

scope :shortlived, -> { where('updated_at < created_at + interval 3 day') }
scope:shortlife,->{where('updated_at
这与此处发布的答案类似。如果您使用的是postgreSQL,您可以将其与“7天”间隔进行比较

scope :shortlived, -> { where("(updated_at - created_at) < '7 days'") }
scope:shortlife,->{where(((更新时间-创建时间)<'7天'))

这与此处发布的答案类似。如果您使用的是postgreSQL,您可以将其与“7天”间隔进行比较

scope :shortlived, -> { where("(updated_at - created_at) < '7 days'") }
scope:shortlife,->{where(((更新时间-创建时间)<'7天'))