Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.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 这里是否需要使用eval?_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 这里是否需要使用eval?

Ruby on rails 这里是否需要使用eval?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在构建一个类,该类将删除特定时间内未更新的记录。例如,我需要删除过去8个月内未更新的所有记录。我有一个工人类,看起来像这样: class ConnectionsCleanerWorker include Sidekiq::Worker sidekiq_options queue: :connections_cleaner, retry: false def perform months = Figaro.env.connection_updated_months_ag

我正在构建一个类,该类将删除特定时间内未更新的记录。例如,我需要删除过去8个月内未更新的所有记录。我有一个工人类,看起来像这样:

class ConnectionsCleanerWorker
  include Sidekiq::Worker

  sidekiq_options queue: :connections_cleaner, retry: false

  def perform
    months = Figaro.env.connection_updated_months_ago
    Connection.where('updated_at < ?', eval("#{months}.months.ago")).destroy_all
  end
end
类连接CleanerWorker
包括Sidekiq::Worker
sidekiq_选项队列::连接\u清理器,重试:false
def执行
months=Figaro.env.connection\u已更新\u个月前
Connection.where('updated_at<?',eval(“{months}.months.ago”)).destroy_all
结束
结束

它工作正常,但我想知道是否有任何方法可以不使用
eval
编写此代码。如果
months
Fixnum
,它将不使用
eval
。在其他情况下(
month
是一个字符串),最好执行以下操作:

def perform
  months_count = Figaro.env.connection_updated_months_ago.to_i
  Connection.where('updated_at < ?', months_count.months.ago)).destroy_all
end
def执行
months\u count=Figaro.env.connection\u已更新\u months\u.to\u i
连接.where('updated_at<?',months_count.months.ago)).destroy_all
结束

否。如果
月数
Fixnum
,则它将在没有
eval
的情况下工作。在其他情况下(
month
是一个字符串),最好执行以下操作:

def perform
  months_count = Figaro.env.connection_updated_months_ago.to_i
  Connection.where('updated_at < ?', months_count.months.ago)).destroy_all
end
def执行
months\u count=Figaro.env.connection\u已更新\u months\u.to\u i
连接.where('updated_at<?',months_count.months.ago)).destroy_all
结束

不,你不需要它。您只需在整数上执行:
3.moths.ago
。这意味着您只需执行
months.months.ago

period = Figaro.env.connection_updated_months_ago
Connection.where('updated_at < ?', period.months.ago)).destroy_all
period=Figaro.env.connection\u已更新\u个月前
连接.where('updated_at<?',period.months.ago)).destroy_all

不,你不需要它。您只需在整数上执行:
3.moths.ago
。这意味着您只需执行
months.months.ago

period = Figaro.env.connection_updated_months_ago
Connection.where('updated_at < ?', period.months.ago)).destroy_all
period=Figaro.env.connection\u已更新\u个月前
连接.where('updated_at<?',period.months.ago)).destroy_all

连接。where('updated_at<?',months.months.ago.)。全部销毁
-这应该可以正常工作。但是
months.months.ago
返回的是什么?为什么首先要使用eval?@max,我假设插值
“#{months}.months.ago
Connection.where('updated_at<?',months.months.ago).destroy_all
-这应该可以。但是,
months.months.ago
返回什么?为什么首先使用eval?@max,我假设插值
“{months}.months.ago