我是随机的(看起来)在我的本地主机上得到一个nil.each错误。对代码感觉很自信,所以我把它推到了heroku,它在那里工作得很好。在mysql2 gem中随机获得随机nil.each错误:include in Rails's find
这里是我的视图代码:
<h2><%[email protected]%></h2>
<br />
<% @book.chapters.each do |chapter| %>
<h3>Chapter <%=chapter.number%></h3>
<% chapter.verses.each do |verse| %>
<b><%=verse.number%>)</b> <%=verse.body%>
<% end %>
<br /><br />
<% end %>
在首先我在控制人是像@book = Book.find(params[:id])
和除了速度工作的罚款。然后,我改变了这个:
def show
if params[:book_name]
#@book = Book.find_by_sql(["select * from books where UPPER(name) = UPPER(?)", params[:book_name]]).first
@book = Book.where(["UPPER(name) = UPPER(?)", params[:book_name]]).includes(:chapters => :verses).first
raise ActiveRecord::RecordNotFound, "Page not found" if @book.nil?
elsif params[:id]
@book = Book.find(params[:id].to_i, :include => {:chapters => :verses})
end
end
现在我随机得到这个错误:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
...
app/controllers/books_controller.rb:14:in `show'
一个区别是Heroku的采用postgre,我用mysql。
Heroku的网址:http://kjv-on-rails.heroku.com/books/1
完整的应用程序代码:https://github.com/tybro0103/KJV-on-Rails
更新 我刚才检查的完整曲线,实现了错误发生在这里:
mysql2 (0.2.6) lib/active_record/connection_adapters/mysql2_adapter.rb:635:in `select'
所以误差在mysql2宝石发生。
注意 它被引起了我的注意,我有奇数/多余的关系&数据。我意识到这一点,我保证!我不相信这是造成错误。 :)
注意 通过随机这里是我的意思......在一些书上,我从来没有看到这个错误。在其他人中,我看到90%的时间,而在其他人中,有50%的时间看到其他人。我只是一遍又一遍地刷新,有时它会起作用,有时它不会。我想真正明白我的意思的唯一方法就是自己下载代码。我的猜测是没有人会这意味着我会在几天内开始奖励作为奖励。 :)
UPDATE
所以错误发生在mysql2适配器。有趣的是,@book = Book.find(params[:id].to_i, :include => {:chapters => :verses})
导致运行rails服务器时出现错误。在rails控制台中,我可以整天运行相同的命令,并且工作正常。它使用webrick,但我尝试切换到杂种,并得到相同的错误。我也尝试从ruby 1.8.7切换到1.9.2,并得到相同的错误。看起来也许在rails服务器中可能存在某种内存限制?
代码看起来不错,没有错误提出http://kjv-on-rails.heroku.com/books/1 – fl00r 2011-03-10 20:54:36
耶正是......但在我的本地我得到一个错误nil.each ...只是找到了类似的问题:http://stackoverflow.com/questions/4536473/find-by-include-nil-object-error-in-rails – tybro0103 2011-03-10 20:56:19
不看类似我 – fl00r 2011-03-10 20:58:46