【问题标题】:Sort Grants by expiration date按到期日期对赠款进行排序
【发布时间】:2016-03-21 20:33:48
【问题描述】:

我有一个应用程序,其中用户有项目并且项目与赠款相匹配。最初我设置了应用程序,以便管理员在创建匹配项时设置交货日期。比赛按交货期显示。

现在我想切换它,以便按到期日期列出赠款(赠款模型上的 expires_at)。我可以列出project.matches,但我似乎无法访问授权的到期日期。所以matches.first.grant.expires_at 获得了第一次授权的到期日期,但我如何在代码中为所有授权匹配执行此操作?

match.rb:

class Match < ActiveRecord::Base
  belongs_to :grant
  belongs_to :project
...
  def self.delivery_date_before(date)
    where("delivery_date  < ? ", date)
  end

  def self.delivery_date_after(date)
    where("delivery_date  >= ? ", date)
  end

  def self.delivery_date_between(from, to)
    where("delivery_date  >= ? and delivery_date <= ?", from, to)
  end

  def match_uniqueness
    if grant_id_changed? || project_id_changed?
      if grant_id.present? && project_id.present? && 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 < Time.zone.now
    else
      false
    end
  end
...
end

项目.rb:

class Project < 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

user.rb:

class User < 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

研究人员仪表板中匹配列表的一个实例。我还有另外两场这样的比赛,用于之前和未来的比赛。

<% if @project.matches.any? %>
    <h3><%= project_title @project %></h3>
    <div class="row">
        <div class="col-md-12 dashboard-panel ">
                <div class="panel panel-default">
                    <div class="panel-heading"><h4>Your grant matches
                        <span class='small'><%= period_in_words %></span>
                    </h4></div>
                    <% if @project.current_matches.any? %>
                        <%= render 'match_table', matches: @project.current_matches %>
                    <% else %>
                        <div class="row">
                            <div class="col-md-6 col-md-offset-3" >
                                <div class='well'>
                                    No matches for this period
                                </div>
                            </div>
                        </div>
                    <% end %>
                </div>
        </div>
    </div>

最后是匹配表:

<table class="table">
  <tr>
    <th>Grant</th>
    <th>Deadline</th>
    <th>Status</th>
    <th>Matching notes</th>
    <th></th>
  </tr>
  <% matches.each do |match| %>
    <tr>
      <td class="match_col"><%= match.grant.name %></td>
      <td class="match_col">
        <%= deadline_in_words match %></td>
      <td class="match_col"><%= match.submission_status %></td>
      <td><%= match.notes.html_safe %></td>
      <td class="match_col center">
        <% if match.submission_active? %>
          <%= link_to "Continue", edit_grant_app_type_submission_path(match.grant, match.app_type, match.submission), class:"btn btn-info"%>
        <% else %>
          <%= link_to 'Apply', match.grant, class:"btn btn-primary" %>
        <% end %>
      </td>
    </tr>
  <% end %>
</table>

我知道我需要更新 self.delivery_date... 方法,但我不确定将其更改为什么。将其切换到截止日期不起作用,因为它在匹配表上寻找一个字段。我觉得我应该可以说@project.matches.grant.expires_at 或@project.matches.grant.each.expires_at。或者可能将 self.deivery_date 更改为:

def self.delivery_date_before(date)
 Grant.where("expires_at  < ? ", date)
end

或

def self.delivery_date_before(date)
 includes.(:grant).where("expires_at  < ? ", date)
end

感谢收看!

更新:

我试过了

def self.expires_before(date)
  includes(:grants).where(:grants => :expires_at >= Date.today)
end

但现在我得到“符号与日期的比较失败”,这感觉就像我得到的最接近的但我不知道为什么 expires_at 会作为一个符号出现,因为它是一个日期时间字段。

【问题讨论】:

    标签: mysql sql-server ruby ruby-on-rails-4


    【解决方案1】:

    你想尝试做这样的事情

    def self.expires_date_before(date)
      where(:grant_id => Grant.where("expires_at <= ?", date))
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-30
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多