我有一个应用程序,用户可以将项目和项目与拨款相匹配。最初我设置了应用程序,以便管理员在创建匹配时设置交付日期。比赛按交货期显示。
现在我想切换它,以便按到期日期(授权模型上的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
project.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会作为符号出现,因为它是一个日期时间字段。
答案 0 :(得分:1)
你想尝试做这样的事情
def self.expires_date_before(date)
where(:grant_id => Grant.where("expires_at <= ?", date))
end