2016-03-21 6 views
3

У меня есть приложение, в котором пользователи проектов и проектов получают соответствие с грантами. Первоначально я настроил приложение, чтобы администратор установил дату доставки при создании соответствия. Матчи показывались по срокам доставки.Сортировка по дате окончания

Теперь я хочу переключить его так, чтобы гранты указывались по дате их истечения (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 

Спасибо за взгляд!

UPDATE:

Я попытался

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

, но теперь я получаю «сравнение символа с датой не удалось» это чувствует себя ближе я получил, но я не знаю, почему expires_at появляется как символ, потому что это поле даты.

ответ

1

вы хотите попробовать сделать что-то вроде этого

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

Это работало отлично! Бесконечно благодарен! – railsie

Смежные вопросы