搜索以小时为整数的数据库池
searching a pool of database with hours as integer
我有一个注册数据库池,其中包含电子邮件、ip、created_at 等属性,我想根据 Time.now 中的小时数进行动态搜索,例如搜索 2 小时前的注册记录,没有对这些时间进行硬编码,但我的解决方案无效且有问题。我怎样才能做到这一点?下面是我做的烂摊子:
traffics_controller.rb
class TrafficsController < BaseController
def show
@search_term = params[:search_term]
@traffic =
if params[:search_term] == 'time_calculated'
SignupHistory.time_calculated
else
SignupHistory.all
end
end
end
型号
class SignupHistory < ActiveRecord::Base
def self.time_calculated
where('created_at > ?', Time.now - "#{time_difference}".hour)
end
end
show.html.slim
# search form rows
.row
.panel.panel-primary
.panel-heading
span = t('.search')
.panel-body
= form_tag admin_statistic_traffics_path, method: :get
.col-xs-4 = text_field_tag :search_term, @search_term
.col-xs-12.col-xs-offset-1 = submit_tag 'Search'
# Table rows
.row
.panel.panel-primary
.panel-heading
span = t('admin_header.traffics')
.panel-body
= table_for(@traffic, class: 'table table-condensed table-hover') do |t|
- t.column :id, 'S/N', class: 'col-xs-1'
- t.column :email, 'Email', class: 'col-xs-2'
- t.column :created_at, 'Signed in Date', class: 'col-xs-3' do |x|
= x.created_at.strftime('%B %e, %Y at %I:%M %p')
- t.column :login_location, 'Login Location', class: 'col-xs-3'
我希望能够在搜索字段中输入 1 并搜索以提取 1 小时前使用 created_at
字段创建的 table 中的记录。但它不起作用。任何帮助将不胜感激。
一个非常简单的解决方案可能是不使用字符串 time_calculated
作为仅 return 最近 SignupHistory
记录的触发器,但实际上该字段中存在一个数字。
# in your controller
def show
@search_term = params[:search_term]
@traffic =
if params[:search_term].to_i > 0
SignupHistory.time_calculated(params[:search_term])
else
SignupHistory.all
end
end
# in your model
def self.time_calculated(number)
where('created_at > ?', number.to_i.hours.ago)
end
我有一个注册数据库池,其中包含电子邮件、ip、created_at 等属性,我想根据 Time.now 中的小时数进行动态搜索,例如搜索 2 小时前的注册记录,没有对这些时间进行硬编码,但我的解决方案无效且有问题。我怎样才能做到这一点?下面是我做的烂摊子:
traffics_controller.rb
class TrafficsController < BaseController
def show
@search_term = params[:search_term]
@traffic =
if params[:search_term] == 'time_calculated'
SignupHistory.time_calculated
else
SignupHistory.all
end
end
end
型号
class SignupHistory < ActiveRecord::Base
def self.time_calculated
where('created_at > ?', Time.now - "#{time_difference}".hour)
end
end
show.html.slim
# search form rows
.row
.panel.panel-primary
.panel-heading
span = t('.search')
.panel-body
= form_tag admin_statistic_traffics_path, method: :get
.col-xs-4 = text_field_tag :search_term, @search_term
.col-xs-12.col-xs-offset-1 = submit_tag 'Search'
# Table rows
.row
.panel.panel-primary
.panel-heading
span = t('admin_header.traffics')
.panel-body
= table_for(@traffic, class: 'table table-condensed table-hover') do |t|
- t.column :id, 'S/N', class: 'col-xs-1'
- t.column :email, 'Email', class: 'col-xs-2'
- t.column :created_at, 'Signed in Date', class: 'col-xs-3' do |x|
= x.created_at.strftime('%B %e, %Y at %I:%M %p')
- t.column :login_location, 'Login Location', class: 'col-xs-3'
我希望能够在搜索字段中输入 1 并搜索以提取 1 小时前使用 created_at
字段创建的 table 中的记录。但它不起作用。任何帮助将不胜感激。
一个非常简单的解决方案可能是不使用字符串 time_calculated
作为仅 return 最近 SignupHistory
记录的触发器,但实际上该字段中存在一个数字。
# in your controller
def show
@search_term = params[:search_term]
@traffic =
if params[:search_term].to_i > 0
SignupHistory.time_calculated(params[:search_term])
else
SignupHistory.all
end
end
# in your model
def self.time_calculated(number)
where('created_at > ?', number.to_i.hours.ago)
end