2016-03-21 10 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 

研究者のダッシュボードから上場試合の一つのインスタンス。私は以前と将来のマッチのために、このような2人の他の人がいます。

<% 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と言うことができるはずです。探して

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 

ありがとう:または可能性のようなものにself.deivery_dateを変更!

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

関連する問題