简体   繁体   English

在Rails的find中使用:include时,在mysql2 gem中获得随机nil.each错误

[英]Getting random nil.each error in mysql2 gem when using :include in Rails's find

I'm randomly (as it seems) getting a nil.each error on my localhost. 我在我的本地主机上随机出现(似乎是)nil.each错误。 Was feeling pretty confident in the code though so I pushed it to heroku and it works fine there. 尽管我对代码感到非常有信心,所以我将其推到了heroku上,并且在这里工作得很好。

Here's my view code: 这是我的查看代码:

<h2><%=@book.name.upcase%></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 %>

At first all I had in my controller was something like @book = Book.find(params[:id]) and that worked fine except for the speed. 起初,我在控制器中所拥有的@book = Book.find(params[:id]) ,除了速度之外,一切都很好。 Then I changed to this: 然后我改为:

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

Now I randomly get this error: 现在我随机得到这个错误:

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'

One difference is heroku uses postgre and I'm using mysql. 一个区别是heroku使用postgre,而我使用mysql。
Heroku url: http://kjv-on-rails.heroku.com/books/1 Heroku网址: http : //kjv-on-rails.heroku.com/books/1
Full application code: https://github.com/tybro0103/KJV-on-Rails 完整的应用程序代码: https : //github.com/tybro0103/KJV-on-Rails

Update I just checked the Full Trace and realized the error occurs here: 更新我刚刚检查了完整跟踪,并意识到错误发生在这里:

mysql2 (0.2.6) lib/active_record/connection_adapters/mysql2_adapter.rb:635:in `select' mysql2(0.2.6)lib / active_record / connection_adapters / mysql2_adapter.rb:635:在'select'中

So the error is happening in the mysql2 gem. 因此,该错误发生在mysql2 gem中。

Note It's been brought to my attention that I have odd/redundant relations & data. 注意引起我注意的是,我有奇数/冗余关系和数据。 I realize this, I promise! 我知道,我保证! I don't believe that's causing the error though. 我不相信这是导致错误的原因。 :) :)

Note By random here's what I mean... On some books I never see this error. 注意这是我的意思。在某些书上,我从未看到此错误。 On others I see 90% of the time, while yet on others I see it 50% of the time. 在其他人上,我看到的机会是90%,而在其他人上,我看到的机会是50%。 I just hit refresh over and over and sometimes it works and sometimes it doesn't. 我只是一遍又一遍地点击刷新,有时它起作用,有时却不起作用。 I guess the only way to really see what I mean is to download the code yourself. 我想真正了解我的意思的唯一方法是自己下载代码。 My guess is no one will which means I'll start a bounty in a couple days as an incentive. 我猜没有人愿意,这意味着我将在几天内开始赏金作为奖励。 :) :)

UPDATE 更新
So the error occurs in the mysql2 adapter. 因此,该错误发生在mysql2适配器中。 Funny thing is that @book = Book.find(params[:id].to_i, :include => {:chapters => :verses}) causes the error when running rails server. 有趣的是@book = Book.find(params[:id].to_i, :include => {:chapters => :verses})在运行Rails服务器时导致错误。 In the rails console I can run the same command all day long and it works fine. 在rails控制台中,我可以整天运行相同的命令,并且效果很好。 It was using webrick, but I tried switching to mongrel and got the same error. 它使用的是webrick,但我尝试切换到mongrel,并得到了相同的错误。 I've also tried switching from ruby 1.8.7 to 1.9.2 and get the same error. 我也尝试从ruby 1.8.7切换到1.9.2,并得到相同的错误。 It seems like maybe there is some sort of memory limit within the rails server maybe? 似乎Rails服务器中可能存在某种内存限制吗?

AHHHH... I found the trouble maker! 啊...我找到了麻烦制造者! I just downgraded mysql from 5.5 to 5.1 ...problem gone! 我刚刚将mysql从5.5降级到5.1 ...问题消失了! What a pain in the @$$ that was. @ $$真是太痛苦了。

It still would be nice to be able to use the latest release of mysql however. 但是,能够使用最新版本的mysql仍然很好。 If someone can find a way to make it work using mysql 5.5 I'll mark that as the accepted answer. 如果有人可以找到一种使用mysql 5.5使其工作的方法,则将其标记为可接受的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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