在第二个数组

时间:2016-02-26 15:36:23

标签: ruby-on-rails arrays ruby ruby-on-rails-4 activerecord

我有一个带有两个日期列的联系人模型。在这种情况下,生日和family_anniversary。当我在接下来的90天内在查询中调用.sort_by时,我会收到以下错误。

错误: undefined method 'birthday_within_90_days' for #<Array:0x007f934fff1dd0>

故障排除: 我已经在每次通话时注释掉了.sort_by,当我这样做的时候,其他的就可以了。我能够弄清楚的最好的是它与ActiveRecord和Arrays有关。我的怀疑是我无法对从同一模型中拉出的两个阵列进行排序。但是我是新手,这是一个我完全处于黑暗中的领域。

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

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

我意识到这不是干的。对于不同的日子,这将是一个不同的问题。

我的控制器代码为:

def dates
  @user = current_user
  contact_ids = @user.contacts.pluck(:id)

  @contacts = Contact
  @contacts = @contacts.where(id: contact_ids.uniq)
  @contacts = @contacts.anniversary_within_90_days
  @contacts = @contacts.birthday_within_90_days
  @children = @user.children
  @pets     = @user.pets
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>

有关如何解决这个问题的任何提示都很受欢迎。

2 个答案:

答案 0 :(得分:0)

您知道何时使用Class和Instance-方法?

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

class Contact
  def birthday_within_90_days
    ..
  end
end 

作品。

作为Class方法编写并使用以下代码

@contacts = Contact.birthday_within_90_days

class Contact
  def self.birthday_within_90_days
    ...
  end
end

答案 1 :(得分:0)

不要做我想做的事

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

相关问题