I'm trying to iterate through my schedules table and get one record with the 'datetime: >= Time.now' to display the current teams next game.
Here's my Team model:
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
Here's my Schedule model:
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
I have a games_helper.rb
module GamesHelper
def current_game
@current_game = current_fan.team.schedules
end
end
I have a partial _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 %>
This seems to work but using return has the array in brackets around the results:
["Sun, Sep 15th, at 4:25 PM", "DEN", " @ ", "NYG"]
Would like it to display:
Sun, Sep 15th, at 4:25 PM, DEN @ NYG
I'm not sure if I'm going about this the right way.
Assuming an ActiveRecord model called 'Game' with a field called 'game_date':
Game.game_date.where('game_date > ?', Time.now).order('game_date').first
This will make sure your database does the sorting and searching, and only returns one record. If you don't like the placeholder syntax, the squeel gem can make it look even more rubyish, though that's probably overkill for this example.
UPDATE (based on changes to the question)
I think you want to move a lot of that ruby code from the partial to your helper. In your helper, add this method:
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
And in your partial just replace the body of the loop that has the above code with:
current_game_scoreboard(game)
You could improve this further by just passing a collection to the scoreboard partial and using Rails' partial magic to do the loop iteration, but this will get you going in the right direction.
You can do this in 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)>
Add a method called next_game
to the Team
model
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
Add a helper to display game info
module GamesHelper
def game_info g
"#{g.datetime.to_s(:custom)}, #{g.away_team.short_name} @ #{g.home_team.short_name}"
end
end
Now in your view:
<% if (game = current_fan.team.next_game).present? %>
<%= game_info(game)%>
<% end %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.