简体   繁体   中英

Array.count works fine locally but breaks on heroku

Right now, if I go to the index action of a model that I have, I don't show the basic table of data that rails generates for me if there are no existing records in the database. I do something like:

<% if @my_records.count > 0 %>
<table>
  <tr>
    <th>...</th>
    <th>...</th>
    <th>...</th>
    <th>etc</th>
  </tr>
  <% @my_records.each do |my_record| %>
  <tr>
    <td><%=h my_record.etc %></td>
    <td><%=h my_record.etc %></td>
    <td><%=h my_record.etc %></td>
    <td><%=h my_record.etc %></td>
  </tr>
  <% end %>
</table>
<% end %>

This works locally. However, when I push my app to heroku, this causes a 500 error and the log says: ActionView::TemplateError (undefined method 'count' for []:Array) on line ...

So I change it to .length and it works fine. Can anyone tell me why that is? Someone has told me that these were redundant and rails got rid of .count but my understanding was that .length is an Array function that tells you how many items are in the Array and .count was an ActiveRecord method for determining how many items in the array were actual records in the database.

Can anyone shed light on this for me?

That's ruby issue, not rails. Locally you probably have 1.8.7, and heroku has 1.8.6. The Enumerable#count method was introduced in 1.8.7: compare http://ruby-doc.org/core-1.8.6/classes/Enumerable.html and http://ruby-doc.org/core-1.8.7/classes/Enumerable.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM