Ruby on rails 如何在我的模型中不重复此代码

Ruby on rails 如何在我的模型中不重复此代码,ruby-on-rails,model,Ruby On Rails,Model,我在一个模型中有这段代码,该模型根据方法运行的时间设置开始日期和结束日期 让我们直接进入代码示例: #MODEL now = Time.now if now.hour >= 17 && now.hour <= 23 #night n = now+1.day startd = Time.local(now.year, now.month, now.day, 17, 00, 00) endd = Time.local(n.year, n.m

我在一个模型中有这段代码,该模型根据方法运行的时间设置开始日期和结束日期

让我们直接进入代码示例:

#MODEL
now = Time.now
if now.hour >= 17 && now.hour <= 23
    #night
    n = now+1.day
    startd = Time.local(now.year, now.month, now.day, 17, 00, 00)
    endd = Time.local(n.year, n.month, n.day, 8, 00, 00)
elsif now.hour >= 0 && now.hour <= 7
    #morning
    n = now-1.day
    startd = Time.local(n.year, n.month, n.day, 8, 00, 00)
    endd = Time.local(now.year, now.month, now.day, 17, 00, 00)     
end
#模型
现在=时间

如果now.hour>=17&&now.hour=0&&now.hour['created_at>=?和created_at,您可以将其包含在
lib
目录的模块中。RAILS\u ROOT/lib中的所有ruby文件都会自动加载

# time_calculations.rb

module TimeCalculations
  def calc_start_and_end_times
    now = Time.now
    if now.hour >= 17 && now.hour <= 23
        #night
        n = now+1.day
        startd = Time.local(now.year, now.month, now.day, 17, 00, 00)
        endd = Time.local(n.year, n.month, n.day, 8, 00, 00)
    elsif now.hour >= 0 && now.hour <= 7
        #morning
        n = now-1.day
        startd = Time.local(n.year, n.month, n.day, 8, 00, 00)
        endd = Time.local(now.year, now.month, now.day, 17, 00, 00)     
    end
    [startd, endd]
  end
end

# model.rb

class Model < ActiveRecord::Base
  extend TimeCalculations

  def self.my_find()
    startd, endd = calc_start_and_end_times
    Model.find(:all, 
      :conditions => ['created_at >= ? AND created_at <= ?', startd, endd])
  end

end
#time_calculations.rb
模块时间计算
def calc_开始和结束时间
现在=时间

如果now.hour>=17&&now.hour=0&&now.hour['created_at>=?和created_at,您可以将其包含在
lib
目录的模块中。RAILS\u ROOT/lib中的所有ruby文件都会自动加载

# time_calculations.rb

module TimeCalculations
  def calc_start_and_end_times
    now = Time.now
    if now.hour >= 17 && now.hour <= 23
        #night
        n = now+1.day
        startd = Time.local(now.year, now.month, now.day, 17, 00, 00)
        endd = Time.local(n.year, n.month, n.day, 8, 00, 00)
    elsif now.hour >= 0 && now.hour <= 7
        #morning
        n = now-1.day
        startd = Time.local(n.year, n.month, n.day, 8, 00, 00)
        endd = Time.local(now.year, now.month, now.day, 17, 00, 00)     
    end
    [startd, endd]
  end
end

# model.rb

class Model < ActiveRecord::Base
  extend TimeCalculations

  def self.my_find()
    startd, endd = calc_start_and_end_times
    Model.find(:all, 
      :conditions => ['created_at >= ? AND created_at <= ?', startd, endd])
  end

end
#time_calculations.rb
模块时间计算
def calc_开始和结束时间
现在=时间

如果now.hour>=17&&now.hour=0&&now.hour['created_at>=?和created_at您可以在模型类中添加方法find_by_date_range(startd,endd)并使用model.find by_date_range(something,something)

您可以在模型类中添加方法find_by_date_range(startd,endd)并使用model.find by_date_范围(某物,某物)

最简单的方法是将代码添加到模块中,可能是在初始值设定项中:

module SmartDates

  def start_date
    now = Time.now
    if now.hour >= 17 && now.hour <= 23
      #night
      n = now+1.day
      return Time.local(now.year, now.month, now.day, 17, 00, 00)
    elsif now.hour >= 0 && now.hour <= 7
      #morning
      n = now-1.day
      return Time.local(n.year, n.month, n.day, 8, 00, 00)
    end
  end

  def end_date
    now = Time.now
    if now.hour >= 17 && now.hour <= 23
      #night
      n = now+1.day
      return Time.local(n.year, n.month, n.day, 8, 00, 00)
    elsif now.hour >= 0 && now.hour <= 7
      #morning
      n = now-1.day
      return Time.local(now.year, now.month, now.day, 17, 00, 00)
    end
  end

end

# Now include your module
ActiveRecord::Base.include(SmartDates)
ActionController::Base.include(SmartDates)
模块智能日期
def开始日期
现在=时间

如果now.hour>=17&&now.hour=0&&now.hour=17&&now.hour=0&&now.hour最简单的方法是将代码添加到模块中,可能是在初始值设定项中:

module SmartDates

  def start_date
    now = Time.now
    if now.hour >= 17 && now.hour <= 23
      #night
      n = now+1.day
      return Time.local(now.year, now.month, now.day, 17, 00, 00)
    elsif now.hour >= 0 && now.hour <= 7
      #morning
      n = now-1.day
      return Time.local(n.year, n.month, n.day, 8, 00, 00)
    end
  end

  def end_date
    now = Time.now
    if now.hour >= 17 && now.hour <= 23
      #night
      n = now+1.day
      return Time.local(n.year, n.month, n.day, 8, 00, 00)
    elsif now.hour >= 0 && now.hour <= 7
      #morning
      n = now-1.day
      return Time.local(now.year, now.month, now.day, 17, 00, 00)
    end
  end

end

# Now include your module
ActiveRecord::Base.include(SmartDates)
ActionController::Base.include(SmartDates)
模块智能日期
def开始日期
现在=时间

如果now.hour>=17&&now.hour=0&&now.hour=17&&now.hour=0&&now.hour这听起来是个不错的解决方案,我已经尝试过了,但它给了我“未定义的方法‘计算开始时间’和结束时间’”错误。我将模块包括在模型类的顶部。有什么想法吗?尝试将def self.calc_start_和def end_时间更改为def calc_start_和def end_时间。我记不起在方法定义中是否需要self。也尝试过了,很抱歉没有提及。听起来模型无法从模块中访问该方法。请删除“self”从模块中的def导入。导入模块时,由于您正试图修改类本身,请使用:class开始。删除“def self.calc”中的self,并使用“extend TimeCalculations”而不是“include TimeCalculations”。当你想使用类方法时,你需要扩展。include是实例方法。这听起来是个不错的解决方案,我已经尝试过了,但它给了我“未定义的方法`calc_start_和`u end_times` for#”错误。我将模块包括在模型类的顶部。有什么想法吗?尝试将def self.calc_start_和def end_时间更改为def calc_start_和def end_时间。我记不起在方法定义中是否需要self。也尝试过了,很抱歉没有提及。听起来模型无法从模块中访问该方法。请删除“self”从模块中的def导入。导入模块时,由于您正试图修改类本身,请使用:class开始。删除“def self.calc”中的self,并使用“extend TimeCalculations”而不是“include TimeCalculations”。当您想要使用类方法时,您需要扩展。include是实例方法。那么,计算开始和结束日期时间的部分在哪里?让我解释一下。首先您需要确定-将在代码的其他地方使用开始/结束计算。如果是-移动负责开始计算的方法将非常有用下一步-在您的模型中开发find方法,该方法将根据您的条件返回所有记录(erik已经输入了一个很好的示例)。还有另一个有用的解决方案-您可以使用named_scope-correction for my comment-“如果是-将负责模块开始和结束计算的方法移动到模块将是有用的,如果不是-则没有必要"那么,计算开始和结束日期时间的部分在哪里呢?让我解释一下。首先,您需要确定-在代码的其他地方是否会使用开始/结束计算。如果是-移动负责开始和结束计算的方法将非常有用。下一步-在模型中开发find方法,该方法将在您的标准(erik已经输入了一个很好的示例)。还有另一个有用的解决方案-您可以使用named_scope-更正我的评论-“如果是-将负责模块开始和结束计算的方法移动将很有用,如果不是-它“不太必要”谢谢,模块代码放在/lib中?包含代码呢,你会把它放在哪里?我可能会把它放在一个初始值设定项中,正如我前面提到的。在
config/initializers/smart_dates.rb
或其他什么东西中。也许我会把模块放在lib/中,把包含代码放在初始值设定项中。谢谢,模块代码放在/lib中?包含代码呢de,你会把它放在哪里?我可能会把它放在一个初始值设定项中,正如我前面提到的。在
config/initializers/smart_dates.rb
或其他什么东西中。也许我会把模块放在lib/中,把include放在初始值设定项中。