Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在第二个数组上调用.sort_by时出现未定义的方法错误_Ruby On Rails_Arrays_Ruby_Ruby On Rails 4_Activerecord - Fatal编程技术网

Ruby on rails 在第二个数组上调用.sort_by时出现未定义的方法错误

Ruby on rails 在第二个数组上调用.sort_by时出现未定义的方法错误,ruby-on-rails,arrays,ruby,ruby-on-rails-4,activerecord,Ruby On Rails,Arrays,Ruby,Ruby On Rails 4,Activerecord,我有一个带有两个日期栏的联系人模型。在这种情况下,生日和家庭纪念日。当我调用.sort\u查询未来90天的日期时,我得到以下错误 错误: 未定义的方法“90天内生日” 故障排除: 我在每次通话中都对.sort_按进行了注释,当我这样做时,另一个也会起作用。我所能理解的最好的情况是,这与ActiveRecord和数组有关。我的怀疑是,我无法对从同一模型中提取的两个数组进行排序。然而,我是新手,这是一个我完全不知道的领域 我的模型代码,请注意,我使用的是RailsLove/birthday gem,

我有一个带有两个日期栏的联系人模型。在这种情况下,生日和家庭纪念日。当我调用.sort\u查询未来90天的日期时,我得到以下错误

错误:
未定义的方法“90天内生日”

故障排除: 我在每次通话中都对.sort_按进行了注释,当我这样做时,另一个也会起作用。我所能理解的最好的情况是,这与ActiveRecord和数组有关。我的怀疑是,我无法对从同一模型中提取的两个数组进行排序。然而,我是新手,这是一个我完全不知道的领域

我的模型代码,请注意,我使用的是RailsLove/birthday gem,这是我获取
方法的
find#{column_name}u的地方

def next_birthday
  year = Date.today.year
  mmdd = birthday.strftime('%m%d')
  year += 1 if mmdd < Date.today.strftime('%m%d')
  mmdd = '0301' if mmdd == '0229' && !Date.parse("#{year}0101").leap?
  return Date.parse("#{year}#{mmdd}")
end

def next_anniversary
  year = Date.today.year
  mmdd = family_anniversary.strftime('%m%d')
  year += 1 if mmdd < Date.today.strftime('%m%d')
  mmdd = '0301' if mmdd == '0229' && !Date.parse("#{year}0101").leap?
  return Date.parse("#{year}#{mmdd}")
end

def self.anniversary_within_90_days
  find_family_anniversaries_for((Date.today), (Date.today + 90.days)).sort_by(&:next_anniversary)
end

def self.birthday_within_90_days
  find_birthdays_for((Date.today), (Date.today + 90.days)).sort_by(&:next_birthday)
end
我的视图代码:

<div data-equalizer class="row">
  <div data-equalizer-watch class="small-12 medium-3 columns">
    <%= render "layouts/sidenav" %>
  </div>
  <div data-equalizer-watch class="medium-9 columns contacts-list show-for-medium-up">
    <ul style="list-style:none;">
      <li>
        <h3>Birthdays in the next 90 Days</h3>
         <% @contacts.each do |contact| %>
          <ul style="list-style:none;">
            <% if contact.birthday.present? %>
              <%= link_to "#{contact.first_name} #{contact.last_name}", contact %> <strong><%= contact.birthday.try(:strftime, "%m/%d/%Y") %></strong> (<%= contact.birthday_age %> years old)
            <% end %>
          </ul>
        <% end %>
      </li>
      <li>
    <h3>Children's Birthdays in the next 90 Days</h3>
    <% @children.each do |child| %>
      <ul style="list-style:none;">
       <% if child.child_birthday.present? %>
        <%= child.name %> <strong><%= child.child_birthday.try(:strftime, "%m/%d/%Y") %></strong> (<%= child.child_birthday_age %> years old)
      <% end %>
      </ul>
    <% end %>
  </li>
  <li>
    <h3>Anniversaries in the next 90 Days</h3>
    <% @contacts.each do |contact| %>
      <ul style="list-style:none;">
        <% if contact.family_anniversary.present? %>
            <%= link_to "#{contact.first_name} #{contact.last_name}", contact %> <strong><%= contact.family_anniversary.try(:strftime, "%m/%d/%Y") %></strong> has been married for <%= contact.family_anniversary_age %> years.
            <% end %>
          </ul>
        <% end %>
      </li>
      <li>
        <h3>Pets Born in the next 90 Days</h3>
        <% @pets.each do |pet| %>
          <ul style="list-style:none;">
            <% if pet.birthday.present? %>
              <%= pet.name %> <strong><%=pet.birthday.try(:strftime, "%m/%d/%Y") %></strong> (<%= pet.birthday_age %> years old)
            <% end %>
          </ul>
        <% end %>
      </li>
    </ul>
  </div>
</div>

  • 未来90天的生日
      (岁)
  • 未来90天的儿童生日
      (岁)
  • 未来90天的周年纪念日
      已经结婚多年了。
  • 未来90天内出生的宠物
      (岁)

如果您有任何关于如何解决此问题的提示,我们将不胜感激。

您知道何时使用类和实例方法吗

@contact = Contact.first # creates an instance of Contact
@contact.birthday_within_90_days

class Contact
  def birthday_within_90_days
    ..
  end
end 
工作

您编写并使用以下代码的as类方法

@contacts = Contact.birthday_within_90_days

class Contact
  def self.birthday_within_90_days
    ...
  end
end

不要做我做的事


这是一个很难闻的代码,从长远来看,它的性能会很差。由于应用程序中有许多日期分布在多个关系中,因此在运行查找和排序时,最好使用单表继承来提高性能。

感谢@devanand的澄清。我使用了Class方法,结果错误消失了,但是它显示了所有联系人的所有值,而不仅仅是用户联系人。第二个列表仍然没有排序。我使用了实例方法(我认为这是正确的方向),我得到了
未定义的方法“90天内的周年纪念”
,因此我的模型中的方法不会通过。对于未来的任何人,正如我在回答中所描述的。但为了清楚起见,请删除self,因为您将该方法作为实例方法调用。
@contacts = Contact.birthday_within_90_days

class Contact
  def self.birthday_within_90_days
    ...
  end
end