0

スケジュール テーブルを反復処理し、'datetime: >= Time.now' で 1 つのレコードを取得して、現在のチームの次の試合を表示しようとしています。

これが私のチームモデルです:

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

ここに私のスケジュールモデルがあります:

class Schedule < ActiveRecord::Base  
  attr_accessible :away_team_id, :datetime, :home_team_id, :season, :week

  belongs_to :away_team, class_name: 'Team'
  belongs_to :home_team, class_name: 'Team'
end

私は games_helper.rb を持っています

module GamesHelper
  def current_game
   @current_game = current_fan.team.schedules
  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 %>

これは機能しているように見えますが、 return を使用すると、結果の周りに括弧で囲まれた配列があります:

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

表示したい:

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

これについて正しい方法で行っているかどうかはわかりません。

4

3 に答える 3

0

あなたはルビーでこれを行うことができます:-

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)>
于 2013-09-06T19:37:06.967 に答える
0

モデルに呼び出さnext_gameれるメソッドを追加するTeam

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 %>
于 2013-09-08T01:08:32.127 に答える