耙子流产了! ActionView :: Template ::错误:未定义的方法'挑战'为nil:NilClass

时间:2016-11-12 06:20:19

标签: ruby-on-rails ruby heroku

我运行这个rake:

each

现在也许在after I run in production方法本身有没有办法不发送给没有任何挑战的用户?

我一直收到这个错误:

  

ActionView :: Template ::错误:未定义的方法'挑战'为nil:NilClass has_many heroku运行rake monthly_report

用户belongs_to挑战并挑战 def monthly_report(user) @user = user if user.email.present? mail to: user.email, subject: "Sorry! Please Ignore | Testing Email - Monthly Challenges Report" end 用户。

user_mailer.rb

Accomplished: <%= @user.challenges.accomplished.count %> Ongoing: <%= @user.challenges.unaccomplished.count %>

    <% if @user.challenges.accomplished.present? %>
        <% @user.challenges.accomplished.order("deadline ASC").each do |challenge| %>
            <% if challenge.date_started != nil %>
                <%= link_to challenge_url(challenge) do %>
                    <%= challenge.name %> <%= challenge.committed_description %> for <%= challenge.days_challenged %> Days
        <% end %>&nbsp;&nbsp;
        <% if challenge.deadline.present? %>
            <span style="display: inline; padding: .2em .6em .3em; font-size: 75%; font-weight: bold; line-height: 1; color: #fff; text-align: center; white-space: nowrap; vertical-align: baseline; border-radius: .25em; background-color: #446CB3; text-decoration: none;"><%= challenge.deadline.strftime("%b %d, %Y") %></span>
        <% end %>
            <% else %>
                <%= link_to challenge_url(challenge) do %>
                    <%= challenge.name %>
                <% end %>&nbsp;&nbsp;
                <% if challenge.deadline.present? %>
                    <span style="display: inline; padding: .2em .6em .3em; font-size: 75%; font-weight: bold; line-height: 1; color: #fff; text-align: center; white-space: nowrap; vertical-align: baseline; border-radius: .25em; background-color: #446CB3; text-decoration: none;"><%= challenge.deadline.strftime("%b %d, %Y") %></span>
                <% end %>
            <% end %>
        <% end %>
    <% else %>
        None.       
    <% end %>

monthly_report.html.erb

add_action( 'pre_get_posts', 'my_change_sort_order'); 

function my_change_sort_order($query){

if(is_archive())
{
    // validate
    if( is_admin() )
    {
        return $query;
    }

    $meta_query = array(
        'relation' => 'OR',
        array( //check meta_key exist
            'key' => 'my_custom_field',
            'compare' => '!=',
            'value' => ''
        ),
        array( //if no meta_key exist
            'key' => 'my_custom_field',
            'compare' => 'NOT EXISTS'
        )
    );
    $query->set( 'meta_query', $meta_query );
    $query->set('meta_key', 'my_custom_field');
    $query->set('orderby', 'meta_value_num');    
    $query->set('order', 'DESC');


    return $query;
}
};

1 个答案:

答案 0 :(得分:2)

请更新您的佣金任务

task monthly_report: :environment do
  User.all.each do |user|
    if user.email.present? && user.challenges.present? && user.challenges.any?
      UserMailer.monthly_report(user).deliver_now  
    end
  end
end

如果您的用户在记录中没有任何电子邮件,我认为错误提升尝试此