Ruby on rails 遍历一个表以显示下一条日期时间为>;=今天,现在

Ruby on rails 遍历一个表以显示下一条日期时间为>;=今天,现在,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.2,我正在尝试遍历我的时间表表,并获得一条“datetime:>=Time.now”记录,以显示下一场比赛的当前球队 以下是我的团队模型: class Team < ActiveRecord::Base attr_accessible :city, :conf, :div, :full_name, :name, :short_name has_many :fans has_many :away_schedules, class_name: 'Schedule', foreign_

我正在尝试遍历我的时间表表,并获得一条“datetime:>=Time.now”记录,以显示下一场比赛的当前球队

以下是我的团队模型:

class Team < ActiveRecord::Base
  attr_accessible :city, :conf, :div, :full_name, :name, :short_name

  has_many :fans
  has_many :away_schedules, class_name: 'Schedule', foreign_key: :away_team_id
  has_many :home_schedules, class_name: 'Schedule', foreign_key: :home_team_id

 def schedules
  (away_schedules + home_schedules).sort_by(&:id)
  end
end
我有一个部分_scoreboard.html.erb

<% current_game.each do |game| %>
  <% if game.datetime.to_s >= Time.now.to_s %>
  <% return current_game = game.datetime.to_s(:custom),
  game.away_team.short_name, " @ ", game.home_team.short_name %>
  <% end %>
<% end %>
希望它显示:

Sun, Sep 15th, at 4:25 PM, DEN @ NYG

我不确定我的方法是否正确。

您可以在ruby中执行此操作:-

require 'date'

dtar = [ "2013-8-15 13:00:00", "2013-9-15 13:00:00","2013-12-15 13:00:00", "2013-12-5 13:00:00"]
dtar.map{|d| Date.parse d}.find{|d| d > Date.today}
# => #<Date: 2013-09-15 ((2456551j,0s,0n),+0s,2299161j)>
需要“日期”
dtar=[“2013-8-15 13:00:00”、“2013-9-15 13:00:00”、“2013-12-15 13:00:00”、“2013-12-5 13:00:00”]
dtar.map{d|Date.parse d}.find{d|d>Date.today}
# => #

假设一个名为“Game”的ActiveRecord模型带有一个名为“Game\u date”的字段:

Game.game_date.where('game_date > ?', Time.now).order('game_date').first
这将确保数据库进行排序和搜索,并且只返回一条记录。 如果您不喜欢占位符语法,squelegem会使它看起来更像rubysh,尽管对于这个例子来说这可能有些过分

更新(基于问题的更改)

我认为您需要将大量ruby代码从partial迁移到helper。在助手中,添加以下方法:

def current_game_scoreboard(game)
  if game.datetime >= Time.now
    current_game = game.datetime.to_s(:custom),
      game.away_team.short_name, " @ ", game.home_team.short_name
    return current_game.join('')
  end
end
在部分代码中,将包含上述代码的循环体替换为:

current_game_scoreboard(game)

您可以通过将集合传递到记分板部分并使用Rails的部分魔术进行循环迭代来进一步改进此功能,但这将使您朝着正确的方向前进。

团队
模型添加一个名为
next\u game
的方法

class Team < ActiveRecord::Base
  def next_game(reload=false)
    @next_game = nil if reload
    @next_game ||= schedules.where('game_date > ?', Time.now).order('game_date').first
  end

  # change the schedules method implementation so that you can perform all the work in db
  def schedules
    Schedule.where("away_team_id = ? OR home_team_id = ?", id, id)
  end
end
现在在你看来:

<% if (game = current_fan.team.next_game).present? %>
  <%= game_info(game)%>
<% end %>


您是否在询问如何从数据库中检索日期大于该值的记录?此外,我们希望看到代码显示您试图解决这个问题!我重写了这个问题。下次我会更详细地描述。谢谢。OP在数据库中有数据,正在使用活动记录检索数据。处理这个问题的适当方法是通过活动记录,这样逻辑就发生在数据库管理器中,而不是Ruby中。对于一张小小的桌子来说,差别可以忽略不计。对于数千行或数百万行来说,这种差异会让Ruby方面的答案屈服,并在行被检索、排序和丢弃时击败DBM和网络,但基于DBM的逻辑甚至不会慢下来。我已经简化了
调度
方法的实现。再试一次。
current_game_scoreboard(game)
class Team < ActiveRecord::Base
  def next_game(reload=false)
    @next_game = nil if reload
    @next_game ||= schedules.where('game_date > ?', Time.now).order('game_date').first
  end

  # change the schedules method implementation so that you can perform all the work in db
  def schedules
    Schedule.where("away_team_id = ? OR home_team_id = ?", id, id)
  end
end
module GamesHelper
  def game_info g
    "#{g.datetime.to_s(:custom)}, #{g.away_team.short_name} @ #{g.home_team.short_name}"
  end
end
<% if (game = current_fan.team.next_game).present? %>
  <%= game_info(game)%>
<% end %>