Reputation: 15374
I would like to perform a query on a returned hash but get undefined method <= on Hash. My query looks like this
<% if @fixture_date <= (Date.today + 7.days) %>
Display fixtures whos date falls in this range
<% else %>
no fixtures
<% end %>
I have form that returns fixtures grouped by date
<%= form_tag controller: 'predictions', action: 'create', method: 'post' do %>
<% @fixture_date.sort.each do |date, fixture| %>
<h5><%= date_format(date) %></h5><br>
<% fixture.each do |fixture|%>
<%= fixture.home_team %> vs <%= fixture.away_team %>
<%= hidden_field_tag "predictions[][home_team]", fixture.home_team %>
<%= hidden_field_tag "predictions[][away_team]", fixture.away_team %>
<%= text_field_tag "predictions[][home_score]" %>
<%= text_field_tag "predictions[][away_score]" %><br />
<%= hidden_field_tag "predictions[][fixture_date]", fixture.fixture_date %>
<%= hidden_field_tag "predictions[][fixture_id]", fixture.id %>
<%= hidden_field_tag "predictions[][user_id]", current_user.id %>
<% end %>
<%= submit_tag "Submit predictions" %>
<% end %>
<% end %>
Example of hash returned
2013-04-17"=>[#<Fixture id: 3, home_team: "Man City", away_team: "Wigan", fixture_date: "2013-04-17", kickoff_time: "19:45", created_at: "2013-04-14 14:09:06", updated_at: "2013-04-14 14:09:06", prediction_id: nil>, #<Fixture id: 4, home_team: "West Ham", away_team: "Man Utd", fixture_date: "2013-04-17", kickoff_time: "19:45", created_at: "2013-04-14 14:09:06", updated_at: "2013-04-14 14:09:06", prediction_id: nil>, #<Fixture id: 5, home_team: "Fulham", away_team: "Chelsea", fixture_date: "2013-04-17", kickoff_time: "20:00", created_at: "2013-04-14 14:09:06", updated_at: "2013-04-14 14:09:06", prediction_id: nil>],
Do i need to query the value of the key, if so how would i go about doing that? Im guessing that if i was getting an array returned then this would work? or am i misunderstanding this?
I then thought about doing this in the controller and got this far
@fixture_date.select{ |key, hash| hash["fixture_date"] <= (Date.today + 7.days) }
But again i seem to get an error, this time
Cant convert String into Integer
Thanks
edit
def new
@prediction = Prediction.new
@fixtures = Fixture.all
@fixture_date = @fixtures.group_by {|fd| fd.fixture_date}
@fixture_date.select{ |k, v| v['fixture_date'].to_date <= (Date.today + 7.days) }
end
View
<%= form_tag controller: 'predictions', action: 'create', method: 'post' do %>
<% @fixture_date.sort.each do |date, fixture| %>
<h5><%= date_format(date) %></h5><br>
<% fixture.each do |fixture|%>
<%= fixture.home_team %> vs <%= fixture.away_team %>
<%= hidden_field_tag "predictions[][home_team]", fixture.home_team %>
<%= hidden_field_tag "predictions[][away_team]", fixture.away_team %>
<%= text_field_tag "predictions[][home_score]" %>
<%= text_field_tag "predictions[][away_score]" %><br />
<%= hidden_field_tag "predictions[][fixture_date]", fixture.fixture_date %>
<%= hidden_field_tag "predictions[][fixture_id]", fixture.id %>
<%= hidden_field_tag "predictions[][user_id]", current_user.id %>
<% end %>
<% end %>
<%= submit_tag "Submit predictions" %>
<% end %>
Upvotes: 0
Views: 102
Reputation: 4340
Try Date.parse:
@fixture_date.select! { |key, hash| Date.parse(hash["fixture_date"]) <= (Date.today + 7.days) }
Upvotes: 2