title: Sort Grants by expiration date
body: <p>I have an app where users have projects and projects get matched with grants.  Originally I set up the app so that admin set a delivery date when creating a match.  Matches were displayed by delivery period. </p>
  
  <p>Now I want to switch it so that grants are listed by their expiration date (expires_at on the grant model).  I can list the <code>project.matches</code> but I cant seem to access the expiration date of the grant.  So <code>matches.first.grant.expires_at</code> gets the expiration date of the first grant but how do I do this in code for all the grant matches?</p>
  
  <p>The match.rb:</p>
  
  <pre><code>class Match &lt; ActiveRecord::Base
    belongs_to :grant
    belongs_to :project
  ...
    def self.delivery_date_before(date)
      where("delivery_date  &lt; ? ", date)
    end
  
    def self.delivery_date_after(date)
      where("delivery_date  &gt;= ? ", date)
    end
  
    def self.delivery_date_between(from, to)
      where("delivery_date  &gt;= ? and delivery_date &lt;= ?", from, to)
    end
  
    def match_uniqueness
      if grant_id_changed? || project_id_changed?
        if grant_id.present? &amp;&amp; project_id.present? &amp;&amp; Match.exists?(grant_id: grant_id, project_id: project_id)
          errors.add :project_id, "already assigned this grant"
        end
      end
    end
  
    def deadline
      grant.expires_at
    end
  
    def expired?
      if deadline.present?
        deadline &lt; Time.zone.now
      else
        false
      end
    end
  ...
  end
  </code></pre>
  
  <p>The project.rb:</p>
  
  <pre><code>class Project &lt; ActiveRecord::Base
    belongs_to :user
  
    has_many :matches
  
    def previous_matches
      range = user.current_period_range
      matches.delivery_date_before(range[0])
    end
  
    def current_matches
      range = user.current_period_range
      matches.delivery_date_between(range[0], range[1])
    end
  
    def future_matches
      range = user.future_period_range
      matches.delivery_date_after(range[0])
    end
  
    def to_s
      project_title
    end
  end
  </code></pre>
  
  <p>The user.rb:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :projects
    has_many :matches, through: :projects
  ...
    def current_period_range
      today = Time.zone.now
        [ DateTime.new(today.year, today.month, today.day), DateTime.new(today.year, (today.month + 2), today.day) ]
    end
  
    def future_period_range
      today = Time.zone.now
        [ DateTime.new(today.year, (today.month + 2), today.day), DateTime.new(today.year, (today.month + 6), today.day) ]
    end
  end
  </code></pre>
  
  <p>One instance of the match listing from the researcher dashboard.  I have two others like this for previous and future matches.  </p>
  
  <pre><code>&lt;% if @project.matches.any? %&gt;
      &lt;h3&gt;&lt;%= project_title @project %&gt;&lt;/h3&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-md-12 dashboard-panel "&gt;
                  &lt;div class="panel panel-default"&gt;
                      &lt;div class="panel-heading"&gt;&lt;h4&gt;Your grant matches
                          &lt;span class='small'&gt;&lt;%= period_in_words %&gt;&lt;/span&gt;
                      &lt;/h4&gt;&lt;/div&gt;
                      &lt;% if @project.current_matches.any? %&gt;
                          &lt;%= render 'match_table', matches: @project.current_matches %&gt;
                      &lt;% else %&gt;
                          &lt;div class="row"&gt;
                              &lt;div class="col-md-6 col-md-offset-3" &gt;
                                  &lt;div class='well'&gt;
                                      No matches for this period
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;% end %&gt;
                  &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>And finally the match table:</p>
  
  <pre><code>&lt;table class="table"&gt;
    &lt;tr&gt;
      &lt;th&gt;Grant&lt;/th&gt;
      &lt;th&gt;Deadline&lt;/th&gt;
      &lt;th&gt;Status&lt;/th&gt;
      &lt;th&gt;Matching notes&lt;/th&gt;
      &lt;th&gt;&lt;/th&gt;
    &lt;/tr&gt;
    &lt;% matches.each do |match| %&gt;
      &lt;tr&gt;
        &lt;td class="match_col"&gt;&lt;%= match.grant.name %&gt;&lt;/td&gt;
        &lt;td class="match_col"&gt;
          &lt;%= deadline_in_words match %&gt;&lt;/td&gt;
        &lt;td class="match_col"&gt;&lt;%= match.submission_status %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= match.notes.html_safe %&gt;&lt;/td&gt;
        &lt;td class="match_col center"&gt;
          &lt;% if match.submission_active? %&gt;
            &lt;%= link_to "Continue", edit_grant_app_type_submission_path(match.grant, match.app_type, match.submission), class:"btn btn-info"%&gt;
          &lt;% else %&gt;
            &lt;%= link_to 'Apply', match.grant, class:"btn btn-primary" %&gt;
          &lt;% end %&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;% end %&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>I know I need to update the <code>self.delivery_date...</code> methods but I'm not sure what to change it to.  Switching it to deadline doesn't work because its looking for a field on the match table.  I feel like I should be able to say <code>@project.matches.grant.expires_at</code>  or like  <code>@project.matches.grant.each.expires_at</code>.  Or possibly change the self.deivery_date to something like:</p>
  
  <pre><code>def self.delivery_date_before(date)
   Grant.where("expires_at  &lt; ? ", date)
  end
  </code></pre>
  
  <p>or</p>
  
  <pre><code>def self.delivery_date_before(date)
   includes.(:grant).where("expires_at  &lt; ? ", date)
  end
  </code></pre>
  
  <p>Thanks for looking!</p>
  
  <p>UPDATE:</p>
  
  <p>I tried </p>
  
  <pre><code>def self.expires_before(date)
    includes(:grants).where(:grants =&gt; :expires_at &gt;= Date.today)
  end
  </code></pre>
  
  <p>but now I'm getting "comparison of Symbol with Date failed" this feels like the closest I've gotten but I don't know why expires_at is coming up as a symbol because it is a datetime field.      </p>