Note that there are some explanatory texts on larger screens.

plurals
  1. POif/else on a returned hash
    text
    copied!<p>I would like to perform a query on a returned hash but get undefined method &lt;= on Hash. My query looks like this</p> <pre><code>&lt;% if @fixture_date &lt;= (Date.today + 7.days) %&gt; Display fixtures whos date falls in this range &lt;% else %&gt; no fixtures &lt;% end %&gt; </code></pre> <p>I have form that returns fixtures grouped by date</p> <pre><code> &lt;%= form_tag controller: 'predictions', action: 'create', method: 'post' do %&gt; &lt;% @fixture_date.sort.each do |date, fixture| %&gt; &lt;h5&gt;&lt;%= date_format(date) %&gt;&lt;/h5&gt;&lt;br&gt; &lt;% fixture.each do |fixture|%&gt; &lt;%= fixture.home_team %&gt; vs &lt;%= fixture.away_team %&gt; &lt;%= hidden_field_tag "predictions[][home_team]", fixture.home_team %&gt; &lt;%= hidden_field_tag "predictions[][away_team]", fixture.away_team %&gt; &lt;%= text_field_tag "predictions[][home_score]" %&gt; &lt;%= text_field_tag "predictions[][away_score]" %&gt;&lt;br /&gt; &lt;%= hidden_field_tag "predictions[][fixture_date]", fixture.fixture_date %&gt; &lt;%= hidden_field_tag "predictions[][fixture_id]", fixture.id %&gt; &lt;%= hidden_field_tag "predictions[][user_id]", current_user.id %&gt; &lt;% end %&gt; &lt;%= submit_tag "Submit predictions" %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>Example of hash returned</p> <pre><code>2013-04-17"=&gt;[#&lt;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&gt;, #&lt;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&gt;, #&lt;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&gt;], </code></pre> <p>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?</p> <p>I then thought about doing this in the controller and got this far</p> <pre><code>@fixture_date.select{ |key, hash| hash["fixture_date"] &lt;= (Date.today + 7.days) } </code></pre> <p>But again i seem to get an error, this time </p> <pre><code>Cant convert String into Integer </code></pre> <p>Thanks</p> <p>edit</p> <pre><code>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 &lt;= (Date.today + 7.days) } end </code></pre> <p>View</p> <pre><code>&lt;%= form_tag controller: 'predictions', action: 'create', method: 'post' do %&gt; &lt;% @fixture_date.sort.each do |date, fixture| %&gt; &lt;h5&gt;&lt;%= date_format(date) %&gt;&lt;/h5&gt;&lt;br&gt; &lt;% fixture.each do |fixture|%&gt; &lt;%= fixture.home_team %&gt; vs &lt;%= fixture.away_team %&gt; &lt;%= hidden_field_tag "predictions[][home_team]", fixture.home_team %&gt; &lt;%= hidden_field_tag "predictions[][away_team]", fixture.away_team %&gt; &lt;%= text_field_tag "predictions[][home_score]" %&gt; &lt;%= text_field_tag "predictions[][away_score]" %&gt;&lt;br /&gt; &lt;%= hidden_field_tag "predictions[][fixture_date]", fixture.fixture_date %&gt; &lt;%= hidden_field_tag "predictions[][fixture_id]", fixture.id %&gt; &lt;%= hidden_field_tag "predictions[][user_id]", current_user.id %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;%= submit_tag "Submit predictions" %&gt; &lt;% end %&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload